您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在python中获取文件中的字节偏移

如何在python中获取文件中的字节偏移

像这样?

file.tell()

返回文件的当前位置,例如stdio的ftell()。

http://docs.python.org/library/stdtypes.html#file- objects

不幸的是,tell()无法运行,因为OP使用的是stdin而不是文件。但是,围绕它构建包装以提供所需的东西并不难。

class file_with_pos(object):
    def __init__(self, fp):
        self.fp = fp
        self.pos = 0
    def read(self, *args):
        data = self.fp.read(*args)
        self.pos += len(data)
        return data
    def tell(self):
        return self.pos

然后,您可以使用它代替:

fp = file_with_pos(sys.stdin)
python 2022/1/1 18:44:50 有293人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶