On 04/03/2011 21:46, [email protected] wrote:
I've implementing this method of reading a file from the end, i.edef seeker(filename): offset = -10 with open(filename) as f: while True: f.seek(offset, os.SEEK_END) lines = f.readlines() if len(lines)>= 2: return lines[-1] offset *= 2 and consistently run into the following error message when Python 3.2 (running under Pyscripter 2.4.1) tries to execute the line f.seek(offset,2) UnsupportedOperation: can't do non-zero end-relative seeks But offset is initialized to -10. Does anyone have any thoughts on what the error might be caused by?
I think it's because the file has been opened in text mode, so there's the encoding to consider. It may be that it's to stop you from accidentally seeking into the middle of a multibyte sequence, but there's nothing to stop you doing that when seeking relative to the start, for example, so it's possibly a pointless restriction. A workaround is not to seek relative to the end. os.path.getsize() will tell you the length of the file. You'll still have to watch out for DecodeError when you read in case the seek was into the middle of a multibyte sequence. A better workaround may be to open in binary mode and decode the bytes explicitly; if there's a DecodeError then discard the first byte and try again, etc. -- http://mail.python.org/mailman/listinfo/python-list
