Question re: strings / f.read() and eof()

Peter Hansen peter at engcorp.com
Thu May 20 19:49:25 EDT 2004


Rob Mitchell wrote:

> I am trying read a file that has hex data, unfortunately some of that 
> data is a \x1A.
> 
> Both f.read() and string[x:y] return a truncated srting once this value 
> is encounered.  

You are on a Windows machine and have opened the file in text
mode (using 'r' or the default argument to open() or file()) rather
than binary mode which is what you need.

On Windows/DOS machines, ASCII 26 (0x1A) is treated as End Of File
and gives the behaviour you noticed.  Use open('myfile', 'rb')
instead.

-Peter



More information about the Python-list mailing list