file read, binary or text mode

Alan G Isaac aisaac at american.edu
Sat Sep 25 21:24:35 EDT 2004


"Roel Schroeven" <rschroev_nospam_ml at fastmail.fm> wrote in message
news:OjW4d.255917$OR1.13371520 at phobos.telenet-ops.be...
> It's safe in the sense that everything goes out exactly as it came in.
> For example, gzip uses binary mode even when compressing text files. The
> files may be text, but gzip doesn't care about that. It doesn't care
> about words, sentences and line endings, but it does care about
> representing exactly the bytes that are in the file.

I think the following is the same question from another angle.
I have an .zip archive of compressed files that
I want to decompress.  Using the zipfile module,
I tried
z=zipfile.ZipFile(local.zip)
for zname in z.namelist():
        localtxtfile='c:/puthere/'+zname
        f=open(localtxtfile,'w')
        f.write(z.read(zname))
        f.close

The original files were all plain text,
created on an unspecified platform.
The files I decompressed this way contained
*two successive* carriage returns
(ASCII 13) at the end of each line.
If I change 'w' to 'wb' I get only one
carriage return at the end of each line.

Why is this extra carriage return added?
My original guess was the using 'w' instead
of 'wb' would be the right action, since the
platform for the original files is unspecified
and the original files are known to be plain text.

Thanks,
Alan Isaac





More information about the Python-list mailing list