Reputation: 1633
>>> file = open('foo.txt')
>>> file.seek(2)
2
>>> file.read(1)
'\n'
>>> file.tell()
4
why didn't the current position move 1 character forward? Isn't '\n'
supposed to be 1 character?
Content of the first 4 lines of the file:
1@
2@
3@
@
PS : I'm on windows.
Upvotes: 0
Views: 3263
Reputation: 414375
Don't try to interpret the value of file.tell()
for a text file -- it is just some opaque number representing the position in the stream (you can pass it to file.seek()
).
In a text file, file.read(1)
reads one Unicode character (codepoint), not byte. Depending on the character encoding used to read the text file, one Unicode codepoint could be from one to four bytes (usually).
The default is universal newlines mode: '\r\n'
, '\r'
, '\n'
are all translated to just '\n'
.
To see the file content as bytes, open in binary mode: 'rb'
. file.tell()
return position in bytes in this case.
b'\0'
(null byte) indicates utf-16 encoding that is common on Windows.
Upvotes: 4