append one file to another

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Jul 12 11:58:00 EDT 2005


On Tue, 12 Jul 2005 07:38:39 -0700, b83503104 at yahoo.com wrote:

> Thanks for the nice suggestions!
> 
> As a side question, you mentioned opening files in binary mode, in case
> the code needs to run under Windows or cross-platform.  What would
> happen otherwise?  Is it an issue of big little endian or some other
> issue?

No, nothing to do with big and little endian issues. It is all to do with
the line delimiter, and possibly the end-of-file marker.

Windows uses '\r\n' as the line delimiter for text files. (Or is it
'\n\r'? I always forget...)

Old-style Macintosh used '\r', and (almost) everything else, including new
Macs running OS X, uses '\n'.

If you open files in text mode, there can be complications due to the
different line endings. To be perfectly frank, I only use Python under
Linux, so I don't have the foggiest idea of just what Bad Things can
happen. I know it is a big problem when using some FTP programs, which
have a tendency to destroy binary programs if you upload/download them in
text mode. 

I just did some experiments here, and can't get anything bad to happen.
But whatever the problem is, my grand-pappy always told me, open the
danged file in binary mode and you can't go wrong.

*wink*

I have found some discussions here:

http://python.active-venture.com/tut/node9.html

"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. This behind-the-scenes modification to file
data is fine for ASCII text files, but it'll corrupt binary data like that
in JPEGs or .EXE files. Be very careful to use binary mode when reading
and writing such files."

and here:

http://zephyrfalcon.org/labs/python_pitfalls.html

This website recommends:

"Solution: Use the correct flags -- 'r' for text mode (even on Unix), 'rb'
for binary mode."

but I've never had any problems using 'rb' for text files under Linux.

I'm also told that Windows uses ctrl-Z as the end-of-file marker, and if
it finds that character in the middle of a text file, it will assume the
file has finished and stop reading. But only in text mode, not binary. I
don't think that's a problem for Linux.


-- 
Steven.




More information about the Python-list mailing list