[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
02/21/2010 11:13 AM 1,433,600 part0003
02/21/2010 11:13 AM 1,433,600 part0004
02/21/2010 11:13 AM 1,433,600 part0005
02/21/2010 11:13 AM 1,433,600 part0006
02/21/2010 11:13 AM 1,433,600 part0007
02/21/2010 11:13 AM 1,433,600 part0008
02/21/2010 11:13 AM 1,433,600 part0009
02/21/2010 11:13 AM 911,872 part0010
10 File(s) 13,814,272 bytes
2 Dir(s) 188,812,328,960 bytes free

Each of these generated part files represents one binary chunk of the file
python-3.1.msi—a chunk small enough to fit comfortably on a floppy disk of the time.
In fact, if you add the sizes of the generated part files given by the ls command, you’ll
come up with exactly the same number of bytes as the original file’s size. Before we see
how to put these files back together again, here are a few points to ponder as you study
this script’s code:


Operation modes
This script is designed to input its parameters in either interactive or command-
line mode; it checks the number of command-line arguments to find out the mode
in which it is being used. In command-line mode, you list the file to be split and
the output directory on the command line, and you can optionally override the
default part file size with a third command-line argument.
In interactive mode, the script asks for a filename and output directory at the con-
sole window with input and pauses for a key press at the end before exiting. This
mode is nice when the program file is started by clicking on its icon; on Windows,
parameters are typed into a pop-up DOS box that doesn’t automatically disappear.
The script also shows the absolute paths of its parameters (by running them
through os.path.abspath) because they may not be obvious in interactive mode.


Binary file mode
This code is careful to open both input and output files in binary mode (rb, wb),
because it needs to portably handle things like executables and audio files, not just
text. In Chapter 4, we learned that on Windows, text-mode files automatically map
\r\n end-of-line sequences to \n on input and map \n to \r\n on output. For true
binary data, we really don’t want any \r characters in the data to go away when
read, and we don’t want any superfluous \r characters to be added on output.
Binary-mode files suppress this \r mapping when the script is run on Windows
and so avoid data corruption.
In Python 3.X, binary mode also means that file data is bytes objects in our script,
not encoded str text, though we don’t need to do anything special—this script’s
file processing code runs the same on Python 3.X as it did on 2.X. In fact, binary
mode is required in 3.X for this program, because the target file’s data may not be
encoded text at all; text mode requires that file content must be decodable in 3.X,
and that might fail both for truly binary data and text files obtained from other


Splitting and Joining Files | 285
Free download pdf