Saturday 5 December 2015

Python: Get file object current position

‘tell’ method returns an integer giving the file object’s current position in the file represented as number of bytes from the beginning of the file when in binary mode and an opaque number when in text mode.
$ cat /Users/harikrishna_gurram/data.txt 
first line
Second line
Third line
Fouth line
Fifth line
Sixth line
Seventh line
Eighth line

>>> f=open("/Users/harikrishna_gurram/data.txt")
>>> f.readline()
'first line\n'
>>> f.tell()
11
>>> 
>>> f.readline()
'Second line\n'
>>> f.tell()
23
>>> 
>>> f.readline()
'Third line\n'
>>> f.tell()
34


Previous                                                 Next                                                 Home

No comments:

Post a Comment