像这样?
file.tell()
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)