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

(yzsuai) #1
[in a pop-up DOS console box when join.py is clicked]
Directory containing part files? c:\temp\parts
Name of file to be recreated? c:\temp\morepy31.msi
Joining c:\temp\parts to make c:\temp\morepy31.msi
Join complete: see c:\temp\morepy31.msi
Press Enter key

Because these scripts package their core logic in functions, though, it’s just as easy to
reuse their code by importing and calling from another Python component (make sure
your module import search path includes the directory containing the PP4E root first;
the first abbreviated line here is one way to do so):


C:\temp> set PYTHONPATH=C:\...\dev\Examples
C:\temp> python
>>> from PP4E.System.Filetools.split import split
>>> from PP4E.System.Filetools.join import join
>>>
>>> numparts = split('python-3.1.msi', 'calldir')
>>> numparts
10
>>> join('calldir', 'callpy31.msi')
>>>
>>> import os
>>> os.system('fc /B python-3.1.msi callpy31.msi')
Comparing files python-3.1.msi and CALLPY31.msi
FC: no differences encountered
0

A word about performance: all the split and join tests shown so far process a 13 MB
file, but they take less than one second of real wall-clock time to finish on my Windows
7 2GHz Atom processor laptop computer—plenty fast for just about any use I could
imagine. Both scripts run just as fast for other reasonable part file sizes, too; here is the
splitter chopping up the file into 4MB and 500KB parts:


C:\temp> C:\...\PP4E\System\Filetools\split.py python-3.1.msi tempsplit 4000000
Splitting C:\temp\python-3.1.msi to C:\temp\tempsplit by 4000000
Split finished: 4 parts are in C:\temp\tempsplit

C:\temp> dir tempsplit
...more...
Directory of C:\temp\tempsplit

02/21/2010 01:27 PM <DIR>.
02/21/2010 01:27 PM <DIR> ..
02/21/2010 01:27 PM 4,000,000 part0001
02/21/2010 01:27 PM 4,000,000 part0002
02/21/2010 01:27 PM 4,000,000 part0003
02/21/2010 01:27 PM 1,814,272 part0004
4 File(s) 13,814,272 bytes
2 Dir(s) 188,671,983,616 bytes free

290 | Chapter 6: Complete System Programs

Free download pdf