python tutorial

Dave Angel davea at ieee.org
Tue Jun 16 01:30:00 EDT 2009


steve wrote:
> I was just looking at the python tutorial, and I noticed these lines:
>
> http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
>
> "Windows makes a distinction between text and binary files;
> "the end-of-line characters in text files are automatically altered
> "slightly when data is read or written.
>
> I don't see any obvious way to at docs.python.org to get that corrected: Is 
> there some standard procedure?
>
> Steve 
>
>   
It's not clear from your question what you want corrected.  Are you 
saying that the tutorial leaves out some detail?  Or are you upset that 
reading the data gets "automatically altered" data?

If it's the former, just lookup the function in the reference 
documentation (eg. the chm file in a Windows installation).

The way to control the behavior is with the 'mode' parameter to open().  
If mode has a 'b' in it, the file is considered binary, which means no 
translation is done.  If the mode has a 'u' in it, or neither 'b' nor 
'u', then some translation is done.  The purpose of the translation is 
to let the program always use \n to mean end of line, for code that'll 
be portable between the various operating system conventions.  Windows 
typically does text files with \r\n at the end of each line.  Some Macs 
do just a \r, and Unix and Linux use a \n.

One reason a programmer has to be aware of it is that he/she may be 
reading or writing a file from a different operating environment, for 
example, a script that'll be uploaded to a web server running a 
different OS.





More information about the Python-list mailing list