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

(yzsuai) #1

if name == 'main':
if len(sys.argv) == 2 and sys.argv[1] == '-help':
print('Use: split.py [file-to-split target-dir [chunksize]]')
else:
if len(sys.argv) < 3:
interactive = True
fromfile = input('File to be split? ') # input if clicked
todir = input('Directory to store part files? ')
else:
interactive = False
fromfile, todir = sys.argv[1:3] # args in cmdline
if len(sys.argv) == 4: chunksize = int(sys.argv[3])
absfrom, absto = map(os.path.abspath, [fromfile, todir])
print('Splitting', absfrom, 'to', absto, 'by', chunksize)


try:
parts = split(fromfile, todir, chunksize)
except:
print('Error during split:')
print(sys.exc_info()[0], sys.exc_info()[1])
else:
print('Split finished:', parts, 'parts are in', absto)
if interactive: input('Press Enter key') # pause if clicked


By default, this script splits the input file into chunks that are roughly the size of a
floppy disk—perfect for moving big files between the electronically isolated machines
of the time. Most importantly, because this is all portable Python code, this script will
run on just about any machine, even ones without their own file splitter. All it requires
is an installed Python. Here it is at work splitting a Python 3.1 self-installer executable
located in the current working directory on Windows (I’ve omitted a few dir output
lines to save space here; use ls -l on Unix):


C:\temp> cd C:\temp

C:\temp> dir python-3.1.msi
...more...
06/27/2009 04:53 PM 13,814,272 python-3.1.msi
1 File(s) 13,814,272 bytes
0 Dir(s) 188,826,189,824 bytes free

C:\temp> python C:\...\PP4E\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp> python C:\...\P4E\System\Filetools\split.py python-3.1.msi pysplit
Splitting C:\temp\python-3.1.msi to C:\temp\pysplit by 1433600
Split finished: 10 parts are in C:\temp\pysplit

C:\temp> dir pysplit
...more...
02/21/2010 11:13 AM <DIR>.
02/21/2010 11:13 AM <DIR> ..
02/21/2010 11:13 AM 1,433,600 part0001
02/21/2010 11:13 AM 1,433,600 part0002

284 | Chapter 6: Complete System Programs

Free download pdf