| 1 |
ninoborges |
8 |
##Joiner upper
|
| 2 |
|
|
import os, sys
|
| 3 |
|
|
readsize = 1024
|
| 4 |
|
|
|
| 5 |
|
|
def join(fromdir, tofile):
|
| 6 |
|
|
output = open(tofile, 'wb')
|
| 7 |
|
|
parts = os.listdir(fromdir)
|
| 8 |
|
|
parts.sort()
|
| 9 |
|
|
for filename in parts:
|
| 10 |
|
|
filepath = os.path.join(fromdir,filename)
|
| 11 |
|
|
fileobj = open(filepath, 'rb')
|
| 12 |
|
|
while 1:
|
| 13 |
|
|
filebytes = fileobj.read(readsize)
|
| 14 |
|
|
if not filebytes: break
|
| 15 |
|
|
output.write(filebytes)
|
| 16 |
|
|
fileobj.close()
|
| 17 |
|
|
output.close()
|
| 18 |
|
|
|
| 19 |
|
|
if __name__=='__main__':
|
| 20 |
|
|
if len(sys.argv) == 2 and sys.argv[1] == 'help':
|
| 21 |
|
|
print 'use: join.py [from-dir-name to0file-name]'
|
| 22 |
|
|
else:
|
| 23 |
|
|
if len(sys.argv) !=3:
|
| 24 |
|
|
interactive = 1
|
| 25 |
|
|
fromdir = raw_input('Directory containing part files? ')
|
| 26 |
|
|
tofile = raw_input('Name of the file to be recreated? ')
|
| 27 |
|
|
else:
|
| 28 |
|
|
interactive = 0
|
| 29 |
|
|
fromdir, tofile = sys.argv[1:]
|
| 30 |
|
|
absfrom, absto = map(os.path.abspath, [fromdir, tofile])
|
| 31 |
|
|
print 'Joining', absfrom, 'to make', absto
|
| 32 |
|
|
try:
|
| 33 |
|
|
join(fromdir, tofile)
|
| 34 |
|
|
except:
|
| 35 |
|
|
print 'error jopining files:'
|
| 36 |
|
|
print sys.exc_type, sys.exc_value
|
| 37 |
|
|
else:
|
| 38 |
|
|
print 'Join complete: see', absto
|
| 39 |
|
|
if interactive: raw_input('press enter key') |