Advertisement
Guest User

Untitled

a guest
May 6th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import subprocess
  2. import os
  3. import hashlib
  4. import shutil
  5. files = []
  6. hashes = {}
  7. comp_hashes = []
  8. move = {}
  9. destfolder = '/volumeUSB1/usbshare/movie/'
  10. sizes = {}
  11. totalsize = 0
  12. def du(path):
  13. if os.path.isdir(path):
  14. return int(subprocess.check_output(['du','-s', path]).split()[0])
  15. else:
  16. return getSize(path)
  17.  
  18. def sizeof_fmt(num, suffix='B'):
  19. for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
  20. if abs(num) < 1024.0:
  21. return "%3.1f%s%s" % (num, unit, suffix)
  22. num /= 1024.0
  23. return "%.1f%s%s" % (num, 'Yi', suffix)
  24.  
  25. def getSize(filename):
  26. st = os.stat(filename)
  27. return st.st_size
  28. if __name__ == '__main__':
  29. with open('hashes.txt', 'r') as f:
  30. for hash in f:
  31. comp_hashes.append(hash.strip())
  32. for file in os.listdir('.'):
  33. files.append(file)
  34. for file in files:
  35. hashes[file] = hashlib.md5(file).hexdigest()
  36. print 'got %d files and %d hashes' % (len(files), len(hashes))
  37. print 'comparing'
  38. for file, hash in hashes.iteritems():
  39. if not hash in comp_hashes:
  40. move[file] = hash
  41.  
  42. for file in move:
  43. sizes[file] = du(file)
  44. totalsize = sum(sizes.values())
  45. print 'need to move %d files (%s)' % (len(move), sizeof_fmt(totalsize))
  46. i = 0
  47. for file, hash in move.iteritems():
  48. i+= 1
  49. print 'moving %d (%s) of %d (%s remaining): %s' % (i, sizeof_fmt(sizes[file]), len(move), sizeof_fmt(totalsize), file)
  50. dest = os.path.join(destfolder, file)
  51. if os.path.exists(dest):
  52. print 'path exists %s' % dest
  53. continue
  54.  
  55. if os.path.isdir(file):
  56. shutil.copytree('./'+file, os.path.join(destfolder, file))
  57. else:
  58. shutil.copy2('./'+file, os.path.join(destfolder, file))
  59. totalsize -= sizes[file]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement