relative seek for io.StringIO in python3

ぐ巨炮叔叔 提交于 2019-12-12 18:24:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!