iuppiter
iuppiter

Reputation: 181

How to find the byte offset in STDIN in Python?

Normally, I would do something like:

f=open(path)

for line in iter(f.readline, ''):

    print f.tell() 

However, seek operations are not possible with:

import sys

for line in iter(sys.stdin.readline, ''):

    print sys.stdin.tell()

Upvotes: 0

Views: 513

Answers (1)

Tong Zhou
Tong Zhou

Reputation: 636

You can calculate file offset yourself.

import sys

offset = 0
for line in sys.stdin:
  print offset
  offset += len(line)

Upvotes: 1

Related Questions