Skript, mis otsib üles duplikaatfailid - Python

From ICO wiki
Revision as of 14:56, 30 January 2011 by Lvosandi (talk | contribs)
Jump to navigationJump to search

Näeb välja nagu copy-pasta: http://stackoverflow.com/questions/2399196/finding-duplicate-files-by-content-across-multiple-directories sellisel juhul oleks tore kui viidataks ka :)

#!/usr/bin/python
# -*- coding: utf-8 -*-
#Skript leidmaks duplikaatfailid
#Chris Sinihelm, AK41, 2010
import hashlib
import os
def sha(filename):
    d = hashlib.sha512()
    try:
        d.update(open(filename).read())
    except Exception,e:
        print e
    else:
        return d.hexdigest()
s={}
path = raw_input ("Sisesta koht, kus kohast duplikaatfaile otsitakse: ")
print "Sisestasid ", path
for r,d,f in os.walk(path):
    for files in f:
        filename=os.path.join(r,files)
        digest=sha(filename)
        if not s.has_key(digest):
            s[digest]=filename
        else:
            print "Järgnevad on duplikaat failid: %s <==> %s " %( filename, s[digest])