Seeing next character in an file

Richie Hindle richie at entrian.com
Mon Jul 28 07:54:57 EDT 2003


[Keith]
> def peek(self, cnt):
>     data = self.read(cnt)
>     self.seek(cnt * -1, 1)
>     return data
> 	
> def peekline(self):
>     pos = self.tell()
>     data = self.readline()
>     self.seek(pos, 0)
>     return data

[Bengt]
> if you're going to seek/tell, best to do it in binary, and deal with the platform dependent EOLs.

seek() works perfectly with text-mode files as long as you only seek to
places given to you by tell().  So if Keith's peek() function had used
tell() and then seek()ed (sought()? 8-) back to that point like his
peekline() does, there would be no problem.

-- 
Richie Hindle
richie at entrian.com






More information about the Python-list mailing list