
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
File pointer एक position होता है, जो हमे दिखाता है कि हम file में किस जगह पर है, python में by default ये pointer किसी file के starting (यानि 0 index) पर होता है।
python में file pointer के position के लिए f.tell() और f.seek() functions का use किया जाता है।
tell() method हमे file pointer का current position बताता है। जब file को open किया जाता है तो pointer का position 0 index (starting) पर होता है, but जब हम file में कुछ read या write करते है तो pointer move हो जाता है, ऐसे में हम tell() method का use करके pointer का current position देख सकते है।
f = open("demo.txt","r")
print(f.read(5)) # first 5 characters read
print("Pointer at:", f.tell()) # ab pointer position 5 pe hai
f.close()
seek() method का use करके हम pointer को किसी specific position पर ले जा सकते है। इसमे 2 parameter होते है-
# File content: "HelloWorld" (10 characters)
# whence = 0 (beginning of file)
with open("demo.txt", "r") as f:
f.seek(3, 0) # start se 3 characters aage - index=3
print("seek(3,0) position:", f.tell())
print("read(4):", f.read(4)) # "loWo"
# whence = 1 (current position)
with open("demo.txt", "r") as f:
print("read(5):", f.read(5)) # "Hello"; pointer now at 5
f.seek(2, 1) # current position (5) + 2 - index=7
print("seek(2,1) position:", f.tell())
print("read(2):", f.read(2)) # "rl"
# whence = 2 (end of file)
with open("demo.txt", "rb") as f: # binary mode recommended
f.seek(-4, 2) # end se 4 bytes peeche - index=6
print("seek(-4,2) position:", f.tell())
print("read():", f.read()) # b"orld"
उम्मीद करते है कि आपको File Pointer अच्छे समझ मे आ गया होगा ये एक File Handling का topic है। अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...
Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules 2.1. math 2.2. random...
Table of Contents All Chapters 1. Iterators 1.1. Iterable 1.2. Iterator 1.3. Iterator Protocol...
Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism 3.1. Polymorphism...
Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance 3.1. Single Inheritance...
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...