Seeing next character in an file

Bengt Richter bokr at oz.net
Sun Jul 27 19:36:17 CEST 2003


On Sun, 27 Jul 2003 02:58:13 GMT, "Keith Jones" <kjones9 at rochester.rr.com> wrote:

>On Sun, 27 Jul 2003 01:09:54 +0000, Grumfish wrote:
>
>> Is there a way to see the next character of an input file object without 
>> advancing the position in the file?
>
>
>To do this, you can do the following:
>
>fin = file('myfile')
>
>... 
>
>char = fin.read(1)
>fin.seek(-1,1)     # set the file's current position back a character
>
Warning, though: this is very iffy on windows unless you have opened the file
in binary. E.g.,

 >>> print >> file('ends_in_windows_EOL.txt','w'), 'Ends in windows EOL here:'

Look at it in binary:
 >>> file('ends_in_windows_EOL.txt','rb').read()
 'Ends in windows EOL here:\r\n'

Cooked:
 >>> file('ends_in_windows_EOL.txt','r').read()
 'Ends in windows EOL here:\n'

Now try to seek back past the apparent \n single character and one more (2)
so we can read the ':'
 >>> f = file('ends_in_windows_EOL.txt')
 >>> f.seek(-2, 2)
 >>> f.read()
 '\n'

Hm. That's a representation of reading the last two characters in cooked mode.
Apparently the seek positioned us to read '\r\n', not a cooked ':\n'

Look at the same in binary:
 >>> f = file('ends_in_windows_EOL.txt', 'rb')
 >>> f.seek(-2, 2)
 >>> f.read()
 '\r\n'

The last two are the windows EOL. Seeking -2 in cooked mode is not positioning at ':\n'
as we can see in the binary:

 >>> f.seek(-3, 2)
 >>> f.read()
 ':\r\n'

[...]

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

Regards,
Bengt Richter




More information about the Python-list mailing list