Search code examples
python-2.7python-3.xseekfseek

f.seek(0, SEEK_END) coming up as NoneType


I have some code that was written for Python 3 that I am trying to make Python 2.7 compatible. In Python 3, the code f.seek(0, SEEK_END) returns 320816 but in Python 2, the same code returns None. When I print the part of the file I'm interested in I get a bunch of lines of the form b'NEUEVLBL\x90\x00ainp16\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' which leads me to believe that the error has something to do with Python 2.7 reading this as a regular string as opposed to a bytes literal and possibly something to do with Python 2.7's use of \ as an escape character (not the case in Python 3). Does anyone have any suggestions?

EDIT: Before anyone suggests it, yes, I've tried 3to2


Solution

  • You're using open() to open the file in 2.7, but that will not return a file object whose seek() method returns the new file pointer location. Use io.open() instead.