file read, binary or text mode

Alan G Isaac aisaac at american.edu
Tue Sep 28 07:31:37 EDT 2004


"Michael Hoffman" <m.h.3.9.1.without.dots.at.cam.ac.uk at example.com> wrote in
message news:cj57cj$1d3$1 at pegasus.csx.cam.ac.uk...
> I imagine the file in the archive was created on a DOS-type system,
> where the line ending is \r\n. That's what you read in. When you write
> it out in "w" mode the \n is expanded to \r\n without checking to see if
> there is already a \r beforehand. So you get \r\r\n.

Thanks; that addresses my basic misconception about writing in textmode.
 I had thought that writing in textmode produced a platform specific
conversion of the text written, but I now understand that this only affects
how \n is written.

> If you want to be able to get "universal newline" input from your
> zipfile, consider piping input through this generator and using "w" mode:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286165

Very helpful.

Thanks,
Alan Isaac





More information about the Python-list mailing list