Does Python support a peek like method for its file objects?
Blair P. Houghton
blair.houghton at gmail.com
Sun Feb 5 03:37:50 EST 2006
Here's a version that should work in text mode as well:
fp = file("some file on disk", "r")
b = ""
while 1:
p = fp.tell()
# peek at the next byte; moves file position only if a byte is read
c = fp.read(1)
# decide whether to read it
if c == "?":
# pretend we never read the byte
fp.seek(p)
break
# now read the byte "for real"
b = c
if not b:
# we've reached the end of the file
break
fp.close()
--Blair
More information about the Python-list
mailing list