It is true, although I've read rumors that file.tell() is slow, at least in text mode. See Click Here for example. You can also define your own wrapper
pos = 0
for lineno, line in enumerate(ifh, 1):
tmp = len(line)
yield lineno, pos, line
pos += tmp
with open(filename, 'rb') as ifh:
for lineno, filepos, line in traverse_file(ifh):
@Gribouillis: yeh, I don't use tell() a lot, but I can image len() being faster. I'm on my phone so I can't check, but I know yours will return the correct character position. What I'm wondering is if tell() will return the correct byte position (multibyte Unicode comes to mind). If so, each version is good for a specific use case, character position versus byte position.
If I am reading so many bytes at a time via read(number_of_bytes) or processing one line at a time via a loop, for line in file how will tell() behave? Will it still be giving me the position in the whole file or will it's positon be relative to what am processing in bytes or the current line?
I'm trying to process a .flac audio file. I need to find 'artist=', under any combination of caps or lowercase. Once I find that in the file how would I get the position of tell() to jump to the end of the '=' sign after 'artists='?
While I'm here, are there new lines in a binary files like a .flac file or is it just one giant line that only shows up as newlines for the screen?
file = open(sys.argv, 'rb')
tag_artist = b'artist='
artist_name = 
line = b''
position = 0
while tag_artist not in line.lower():
line = file.read(15)
Please do not mention other libraries that can do this for me.