fob.tell()
Returns the current position of the file object.
fob=open('data1.txt','r')
print(fob.tell()) # 0
fob=open('data3.txt','w')
fob.write("Hello")
print(fob.tell()) # 5
fob.close()
We will add offset by using seek()
fob=open('data.txt','r')
fob.seek(5)
print(fob.tell()) # 5
By using the data.txt file given at file read()
fob=open('data.txt','br')
fob.seek(-5,2)
print(fob.tell()) # 76
fob=open('data.txt','r')
print(fob.readline())
print(fob.tell())
fob.close()
Output
This is first line
20
with open('data.txt', 'r') as f:
f.readline() # Read first line
print(f.tell()) # Position after first line
f.readline() # Read second line
print(f.tell()) # Position after second line
Output:
Position after first line
Position after second line
with open('binary_file.bin', 'rb') as f:
f.read(10) # Read 10 bytes
print(f.tell()) # Output: 10
with open('example.txt', 'r') as f:
f.seek(5) # Move the file pointer to the 5th byte
print(f.tell()) # Output: 5
Output:
5
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.