Reading files

Chris Barker chrishbarker at attbi.com
Wed Dec 19 15:05:53 EST 2001


Preben wrote:
> I've noticed when I read/make a file on linux-based systems, they get messed
> up on win-systems.
> 
> Sombody told me that it's the 'end-string' char thats different... Is this
> true?

This is either:
A) Nothing to do with Python. The text file line ending convention is
different on *nix and Windows, and you will have to deal with that in
some way whenever you transfer a file between them.

or B) A Python issue, in which case it might have to do with binary vs.
text files in Python. If a file is opened as a text file in Python (the
default) the line endings are converted on read and write to/from
whatever is native to the system running Python to/from a single "\n"
internally to Python. On *nix the line ending converntion is allready
"\n" so nothing is done. This means that on *nix, there is no difference
between binary and text files, so folks sometimes open binary files as
text, and don't have a problem. If that same script is run on Windows
(or Mac, or...) it will fail, because of the text file line feed
translation. To open a file as binary, you use the 'b' flag:
open(filename,'b') or open(filename,'wb')

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at attbi.net                ---           ---           ---
                                     ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list