问题
I am trying to refactor a python 2 package for use with python-3.x. The package uses StringIO.StringIO
under python 2 and makes some use of the object's relative seek method, with statements like flob.seek(-1, 1)
. Unfortunately, the seek
method of the corresponding io.StringIO
object in python 3 does not support relative seeks, so the code raises
OSError: Can't do nonzero cur-relative seeks
when trying to execute that statement.
What is the best way to refactor modules containing these calls, given that I would like to be able to continue using the functions this appears in for file objects as well as (objects derived from) strings?
回答1:
Because strings in Python 2 are renamed bytes in Python 3, the code should use io.BytesIO in Python 3, which supports relative seeking.
来源:https://stackoverflow.com/questions/19981996/relative-seek-for-io-stringio-in-python3