string reading bug in ActiveState Python 2.2 ?

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Sep 3 11:05:33 CEST 2002


Axel Kowald <kowald at molgen.mpg.de> wrote in 
news:3D74757D.1010405 at molgen.mpg.de:

> I'm using ActiveStates Python 2.2.1 under win2000 to read in the 
> attached text file and it seems that python is only reading part of the 
> string. After
> 
> fp = open("bla.txt")
> z = fp.read()
> print len(z), z
> 
> I see that only 11 of the 15 characters have been read. I know they are 
> non-printable characters, but that shouldn't matter, should it ?  Using 
> another python on one of our unix machines everything works as expected 
> and all 15 chars are read in.  Is that there something special about 
> windows that I should know, or is this a bug in ActiveStates Python ?

Python is doing exactly what you asked it to do.

Please read the documentation for the 'file' builtin (of which 'open' is an 
alias):

'''Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' 
truncates the file). Append 'b' to the mode to open the file in binary 
mode, on systems that differentiate between binary and text files (else it 
is ignored). If the file cannot be opened, IOError is raised.

If mode is omitted, it defaults to 'r'. When opening a binary file, you 
should append 'b' to the mode value for improved portability. (It's useful 
even on systems which don't treat binary and text files differently, where 
it serves as documentation.)'''

Windows is a system that differentiates between binary and text files, so 
without the 'b' in the mode argument you are asking the system to read the 
file in text mode. In text mode Windows will drop carriage return 
characters when followed by linefeed, and will stop reading when it hits a 
control-Z character.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list