Seeing next character in an file

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

On Sun, 27 Jul 2003 02:58:13 GMT, "Keith Jones" <kjones9 at> 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 =
>,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'

 >>> 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')
 >>>, 2)

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')
 >>>, 2)

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

 >>>, 2)


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

Bengt Richter

More information about the Python-list mailing list