Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Python file.tell() giving strange numbers?

$
0
0

I am using Python 3.3.0, on windows 64bit.

I have a text file as shown below: (see bottom for download link at mediafire)

hello-data1:blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah-data2:blah blah blah blah blah blah blah blah blah blah blah-data3: Empty-data4: Empty

I'm trying to navigate around the file, and thus I use .tell() to figure out what my position is. However, when reading through the lines of the file as shown below, I get a very strange result:

f=open("test.txt")while True:    a = f.readline()    print("{}    {}".format(repr(a),f.tell()))    if a == "":        break

The result:

'hello\n'    7'\n'    9'-data1:blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n'    18446744073709551714'\n'    99'\n'    101'-data2:blah blah blah blah blah blah blah blah blah blah blah\n'    164'-data3: Empty\n'    179'\n'    181'-data4: Empty'    194''    194

What's with the 18446744073709551714 for the 3rd line? Though it looks like an impossible value, f.seek(18446744073709551714) is an acceptable value that apparently does bring me to the end of the 3rd line. Though, I can't seem to figure out why.

EDIT:Opening in binary mode gives no problems with tell():

f=open("test.txt","rb")while True:    a = f.readline()    print("{}    {}".format(repr(a),f.tell()))    if a == b"":        break

The result:

b'hello\r\n'    7b'\r\n'    9b'-data1:blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah\r\n'    97b'\r\n'    99b'\r\n'    101b'-data2:blah blah blah blah blah blah blah blah blah blah blah\r\n'    164b'-data3: Empty\r\n'    179b'\r\n'    181b'-data4: Empty'    194b''    194

The test.txt text file is downloadable here, just a tiny 194 bytes: http://www.mediafire.com/?1wm4lujb2j48y23


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>