file processing question

Peter Hansen peter at engcorp.com
Mon Sep 2 11:53:48 EDT 2002


Axel Grune wrote:
> 
> Hi, Im trying to read the data of a file with following method
> 
> file = open( inputfile, "r")
> data = file.read()
> file.close()
> 
> but 'data' is always smaller then the file size, e.g. I have a file
> which is 25677824 bytes but file.read() reads only 4295 (file.tell() =
> 25677824).
> Im using python 2.2 under win2k.

Jere gave you the solution, but not the precise reason.  Under
Windows, the Ctrl-Z character (ASCII 26) is treated as End Of File
and causes the read to terminate if you open the file in "text"
mode instead of "binary" mode.  It also causes CR/LF combinations
(ASCII 13 and 10, respectively) to be combined into a single
"newline" character which is actually LF again.  If your file
is text but contains Ctrl-Z, you'll have to use binary to read
it in, but if you still want the line-ending conversion to be
performed you'll have to do it yourself.

-Peter



More information about the Python-list mailing list