Home >> Python Tutorial >> File Pointer
Edit Template
Edit Template

File Pointerr in Hindi

What is File Pointer in Python

File pointer एक position होता है, जो हमे दिखाता है कि हम file में किस जगह पर है, python में by default ये pointer किसी file के starting (यानि 0 index) पर होता है।

File Pointer Methods

python में file pointer के position के लिए f.tell() और f.seek() functions का use किया जाता है।

tell() Method

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 देख सकते है।

🐍
Example 1.py
Copy to clipboard
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

seek() method का use करके हम pointer को किसी specific position पर ले जा सकते है। इसमे 2 parameter होते है-

  1. offset : हमे pointer को file में कितने bytes move करना है वो argument (number) हमे इसमे देंगे।
  2. whence : ये optional होता है जिसका by default value 0 होता है मतलब pointer file में beginning से move होगा। अगर हम इसमे 1 देंगे तो pointer अपने current position से move होगा, और अगर हमे 2 देंगे तो pointer उस file में end से move करेगा।
🐍
Example 2.py
Copy to clipboard
# 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 करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Python Modules

Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules      2.1. math      2.2. random...

Python Iterators

Table of Contents All Chapters 1. Iterators      1.1. Iterable      1.2. Iterator      1.3. Iterator Protocol...

Python Polymorphism

Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism      3.1. Polymorphism...

Python Inheritance

Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance      3.1. Single Inheritance...

Python Function

Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...

Python Dictionary

Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...

Edit Template
Scroll to Top