file read, binary or text mode

Ralf Schmitt ralf at brainbot.com
Fri Sep 24 21:24:04 CEST 2004


"Terry Reedy" <tjreedy at udel.edu> writes:

>
> Newbies, ignore this confusion.
>
> On Windows, text mode autoconverts \r\n to \n on input and viceverse on 
> output.  I believe that that is all the difference.  Period.
>

That's not quite the case. As always windows sucks big time:

$ cat bla.py
open("b.txt", "w").write("bla\x1a")
print len(open("b.txt", "rb").read())
open("b.txt", "a+")
print len(open("b.txt", "rb").read())

ralf at CRACK ~
$ python bla.py
4
3


The last character gets stripped if it's 0x1a when opening a file for
appending in text mode. I remember this from a posting on the metakit
mailing list. The poor guy corrupted his databases while he wanted to
check for write access:
http://www.equi4.com/pipermail/metakit/2003-October/001497.html

- Ralf

-- 
brainbot technologies ag
boppstrasse 64 . 55118 mainz . germany
fon +49 6131 211639-1 . fax +49 6131 211639-2
http://brainbot.com/  mailto:ralf at brainbot.com



More information about the Python-list mailing list