Finding duplicate files and removing them

后端 未结 8 1730
谎友^
谎友^ 2020-11-27 09:26

I am writing a Python program to find and remove duplicate files from a folder.

I have multiple copies of mp3 files, and some other files. I am using the sh1 algorit

8条回答
  •  日久生厌
    2020-11-27 10:16

    I wrote one in Python some time ago -- you're welcome to use it.

    import sys
    import os
    import hashlib
    
    check_path = (lambda filepath, hashes, p = sys.stdout.write:
            (lambda hash = hashlib.sha1 (file (filepath).read ()).hexdigest ():
                    ((hash in hashes) and (p ('DUPLICATE FILE\n'
                                              '   %s\n'
                                              'of %s\n' % (filepath, hashes[hash])))
                     or hashes.setdefault (hash, filepath)))())
    
    scan = (lambda dirpath, hashes = {}: 
                    map (lambda (root, dirs, files):
                            map (lambda filename: check_path (os.path.join (root, filename), hashes), files), os.walk (dirpath)))
    
    ((len (sys.argv) > 1) and scan (sys.argv[1]))
    

提交回复
热议问题