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

PythonServer Side ProgrammingProgramming

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'.

raja
Published on 22-Dec-2017 07:16:21
Advertisements