ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/PS_Split_join.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: 3663 byte(s)
Log Message:
Initial Import

File Contents

# User Rev Content
1 ninoborges 8 ## PS_Split_join.py
2     ## This is a program created for Practice support that splits large files into smaller chunks,
3     ## so that they can be easily transfered to clients.
4     ## EBorges
5     ## 12.11.02
6    
7     import sys, os
8     kilobytes = 1024
9     megabytes = kilobytes * 1000
10     #chunksize = int(1.4 * megabytes)
11    
12     def SplitFile():
13     os.system('cls')
14     print "You have elected to split a file \n "
15     fromfile = raw_input('File to be split? (Full path please.): ')
16     todir = raw_input('Directory to store file parts?: ')
17     chunksize = raw_input('Size in MB to split the file into?: ')
18     chunksize = int(chunksize)
19     chunksize = chunksize * megabytes
20     absfrom, absto = map(os.path.abspath, [fromfile, todir])
21     print 'Splitting', absfrom, 'to', absto, 'by', chunksize
22     print 'This may take a minute or two...'
23     try:
24     parts = split(fromfile, todir, chunksize)
25     except:
26     print 'There has been an error during spliting \nPlease notify Emanuel Borges for assistance'
27     print sys.exc_type, sys.exc_value
28     else:
29     print 'split finished: ', parts, 'parts are in', absto
30     bytefile = todir + '\\Bytesize.dat'
31     print bytefile
32     chunkWrite = str(chunksize)
33     o=open(bytefile,'w')
34     o.write(chunkWrite)
35     o.close()
36     raw_input('Press Enter key to exit')
37    
38     def JoinFile():
39     os.system('cls')
40     print "You have elected to join a file \n "
41     fromdir = raw_input('Directory containing file parts?: ')
42     tofile = raw_input('Name of the file to be recreated? (Full path Please.): ')
43     bytefile = fromdir + r'\Bytesize.dat'
44     f=open(bytefile, 'r')
45     readsize = f.read()
46     readsize = int(readsize)
47     absfrom, absto = map(os.path.abspath, [fromdir, tofile])
48     print 'Joining', absfrom, 'to make', absto
49     try:
50     join(fromdir, tofile, readsize)
51     except:
52     print 'There has been an error during joining \nPlease notify Emanuel Borges for assistance'
53     print sys.exc_type, sys.exc_value
54     else:
55     print 'Join complete: see', absto
56    
57     def split(fromfile, todir, chunksize):
58     if not os.path.exists(todir):
59     os.mkdir(todir)
60     else:
61     for fname in os.listdir(todir):
62     os.remove(os.path.join(todir, fname))
63     partnum = 0
64     input = open(fromfile, 'rb')
65     while 1:
66     chunk = input.read(chunksize)
67     if not chunk: break
68     partnum = partnum+1
69     filename = os.path.join(todir, ('part%04d' %partnum))
70     fileobj = open(filename, 'wb')
71     fileobj.write(chunk)
72     fileobj.close()
73     input.close()
74     assert partnum <= 9999
75     return partnum
76    
77     #readsize = 1024
78    
79     def join(fromdir, tofile, readsize):
80     output = open(tofile, 'wb')
81     parts = os.listdir(fromdir)
82     parts.sort()
83     for filename in parts:
84     filepath = os.path.join(fromdir,filename)
85     fileobj = open(filepath, 'rb')
86     while 1:
87     filebytes = fileobj.read(readsize)
88     if not filebytes: break
89     output.write(filebytes)
90     fileobj.close()
91     output.close()
92    
93     if __name__ == '__main__':
94     selection = 0
95     os.system('cls')
96     while selection < 1 or selection >2:
97     print "Welcome to the McGuireWoods LLP file split and join program"
98     selection = input('Please select for the following two options: \n 1 Split a file \n 2 Join a set of files \n')
99     if selection == 1:
100     SplitFile()
101     if selection == 2:
102     JoinFile()
103     if selection >2 or selection <1:
104     print 'Please select from one of the options above by selecting 1 or 2 \n Try Again \n \n'