ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/learning/Split_Files.py
Revision: 8
Committed: Sat May 5 04:21:19 2012 UTC (13 years, 10 months ago) by ninoborges
Content type: text/x-python
File size: 1649 byte(s)
Log Message:
Initial Import

File Contents

# Content
1 ##Splitter upper
2
3 import sys, os
4 kilobytes = 1024
5 megabytes = kilobytes * 1000
6 chunksize = int(1.4 * megabytes)
7
8 def split(fromfile, todir, chunksize=chunksize):
9 if not os.path.exists(todir):
10 os.mkdir(todir)
11 else:
12 for fname in os.listdir(todir):
13 os.remove(os.path.join(todir, fname))
14 partnum = 0
15 input = open(fromfile, 'rb')
16 while 1:
17 chunk = input.read(chunksize)
18 if not chunk: break
19 partnum = partnum+1
20 filename = os.path.join(todir, ('part%04d' %partnum))
21 fileobj = open(filename, 'wb')
22 fileobj.write(chunk)
23 fileobj.close()
24 input.close()
25 assert partnum <= 9999
26 return partnum
27
28 if __name__ == '__main__':
29 if len(sys.argv) == 2 and sys.argv[1] == 'help':
30 print 'Use: split.py [file-to-split target-dir [chunksize]]'
31 else:
32 if len(sys.argv) <3:
33 interactive = 1
34 fromfile = raw_input('File to be split? ')
35 todir = raw_input('Directory to store part files? ')
36 else:
37 interactive = 0
38 fromfile, todir = sys.argv[1:3]
39 if len(sys.argv) == 4: chunksize = int(sys.argv[3])
40 absfrom, absto = map(os.path.abspath, [fromfile, todir])
41 print 'Splitting', absfrom, 'to', absto, 'by', chunksize
42 try: parts = split(fromfile, todir, chunksize)
43 except:
44 print 'Error during split'
45 print sys.exc_type, sys.exc_value
46 else:
47 print 'split finished: ', parts, 'parts are in', absto
48 if interactive: raw_input('press enter key')