Reputation: 4550
Suppose I'm building a parser, and I want to lookahead in the stream.
In Python 2, I could write:
def peek():
next = inputfile.read(1)
inputfile.seek(-1,1)
return next
however, in Python 3, relative seeks were disabled.
Upvotes: 3
Views: 2773
Reputation: 1515
This does not work in text mode, but it does work in binary mode:
>>> open('test', 'rb').peek(1)
b'test\n'
Upvotes: 3