Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import os
- import hashlib
- import shutil
- files = []
- hashes = {}
- comp_hashes = []
- move = {}
- destfolder = '/volumeUSB1/usbshare/movie/'
- sizes = {}
- totalsize = 0
- def du(path):
- if os.path.isdir(path):
- return int(subprocess.check_output(['du','-s', path]).split()[0])
- else:
- return getSize(path)
- def sizeof_fmt(num, suffix='B'):
- for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
- if abs(num) < 1024.0:
- return "%3.1f%s%s" % (num, unit, suffix)
- num /= 1024.0
- return "%.1f%s%s" % (num, 'Yi', suffix)
- def getSize(filename):
- st = os.stat(filename)
- return st.st_size
- if __name__ == '__main__':
- with open('hashes.txt', 'r') as f:
- for hash in f:
- comp_hashes.append(hash.strip())
- for file in os.listdir('.'):
- files.append(file)
- for file in files:
- hashes[file] = hashlib.md5(file).hexdigest()
- print 'got %d files and %d hashes' % (len(files), len(hashes))
- print 'comparing'
- for file, hash in hashes.iteritems():
- if not hash in comp_hashes:
- move[file] = hash
- for file in move:
- sizes[file] = du(file)
- totalsize = sum(sizes.values())
- print 'need to move %d files (%s)' % (len(move), sizeof_fmt(totalsize))
- i = 0
- for file, hash in move.iteritems():
- i+= 1
- print 'moving %d (%s) of %d (%s remaining): %s' % (i, sizeof_fmt(sizes[file]), len(move), sizeof_fmt(totalsize), file)
- dest = os.path.join(destfolder, file)
- if os.path.exists(dest):
- print 'path exists %s' % dest
- continue
- if os.path.isdir(file):
- shutil.copytree('./'+file, os.path.join(destfolder, file))
- else:
- shutil.copy2('./'+file, os.path.join(destfolder, file))
- totalsize -= sizes[file]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement