Computer >> Computer tutorials >  >> Programming >> Python

How to know the current position within a file in Python?


You can get the current position of the file object using the tell method. For example, if there is a file called my_file with text Hello\nworld,

f = open('my_file.txt', 'r')
f.readline()
print f.tell()
f.close()

The above code will give the output as 6 as it would be pointing just at the beginning of the word 'world'.