This is almost the same question as How to solve "OSError: telling position disabled by next() call". While the older question has received a few answers with useful workarounds, the meaning of the error is not clear. I wonder if anybody can comment on this.
I am learning Python and loosely following a tutorial. I entered the following interactively on Fedora 23:
$ python3
Python 3.4.3 (default, Aug 9 2016, 15:36:17)
[GCC 5.3.1 20160406 (Red Hat 5.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("myfile","r") as file:
... for l in file:
... print("Next line: \"{}\"".format(l))
... print("Current position {:4d}".format(file.tell()))
myfile
contains a few lines of text. The output:
Next line: "This is line number 0
"
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
OSError: telling position disabled by next() call
Googling for this error yields a whopping 6 results. The same happens with Python 3.6.4 on Cygwin on Windows 10.
Edit:
The tell()
method for text files is documented as follows:
Return the current stream position as an opaque number. The number does not usually represent a number of bytes in the underlying binary storage.
"Opaque number" seems to indicate that I can't just print it. So, I replaced the second print()
call with pos = file.tell()
. Same result.