Sixem

Python Scan Directory For Duplicates

Nov 21st, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. #!/usr/bin/python
  2. #Usage: dupe.py "/folder/to/scan/for/duplicates"
  3. #Options: -d (delete duplicate files)
  4. import sys
  5. import os
  6. bytes = filenames = []
  7. x = d = delete = fd = 0
  8. darg = 'null'
  9. try:
  10.     darg = str(sys.argv[2])
  11. except IndexError:
  12.     darg = 'null'
  13. if darg == "-d":
  14.     print ("OPTIONS: -d (deletion) is enabled")
  15.     delete = 1
  16.     print ("")
  17. for root, dirs, files in os.walk(str(sys.argv[1])):
  18.     for file in files:
  19.         p = os.path.join(root,file)
  20.         currfile = os.path.abspath(p)
  21.         filesize = os.path.getsize(currfile)
  22.         if filesize in bytes:
  23.             print ("%s - Duplicate > Matching: %s" % (file, filenames[bytes.index(filesize)]))
  24.             d += 1
  25.             if delete == 1:
  26.                 os.remove(currfile)
  27.                 print ("ACTION: Removed %s" % (file))
  28.         else:
  29.             bytes.append(filesize)
  30.             filenames.append(file)
  31.         x += 1
  32. print ("")
  33. if delete == 1:
  34.     print ("DONE: Scanned %s file(s) and deleted %s duplicate(s)" % (x, d))
  35. else:
  36.     print ("DONE: Scanned %s file(s) and found %s duplicate(s)" % (x, d))
Advertisement
Add Comment
Please, Sign In to add comment