# -*- coding: utf-8 -*- import os import sys import shutil import subprocess import uuid import hashlib defaultencoding = 'gbk' s=os.sep root="D:\code\\test" dst="E:\test" list= os.listdir(root) def md5sum(filename): fd = open(filename,"rb") fcont = fd.read() fd.close() fmd5 = hashlib.md5(fcont).hexdigest() return fmd5 def sha1sum(filename): fd = open(filename,"rb") fcont = fd.read() fd.close() fsha1 = hashlib.sha1(fcont).hexdigest() return fsha1 if sys.getdefaultencoding() != defaultencoding: reload(sys) sys.setdefaultencoding(defaultencoding) def recursive(root,i): if os.path.isdir(os.path.join(root, i)): root=root+"\\"+i list=os.listdir(root) for i in list: su1 = uuid.uuid4() su=str(su1) if os.path.isfile(os.path.join(root,i)): a=i if a.endswith(".php"): ma = sha1sum("" + root + "\\" + a) ma1 = str(ma) print ma1 if os.path.exists('' + dst + '\\php\\' + ma1 + '.php'): shutil.move('' + dst + '\\php\\' + ma1 + '.php','C:\\dd') pass else: print "\t\thash.sha1(0, filesize) == " + "\"" + ma1 + "\"" + " or \r\n\t\t" shutil.copy("" + root + "\\" + a, '' + dst + '\\php\\' + ma1 + '.php') if a.endswith(".jsp"): ma = sha1sum("" + root + "\\" + a) ma1 = str(ma) if os.path.exists('' + dst + '\\jsp\\' + ma1 + '.jsp'): pass else: print "\t\thash.sha1(0, filesize) == " + "\"" + ma1 + "\"" + " or \r\n\t\t" shutil.copy("" + root + "\\" + a, '' + dst + '\\jsp\\' + ma1 + '.jsp') if a.endswith(".asp"): ma = sha1sum("" + root + "\\" + a) ma1 = str(ma) print ma1 if os.path.exists('' + dst + '\\asp\\' + ma1 + '.asp'): pass else: print "\t\thash.sha1(0, filesize) == " + "\"" + ma1 + "\"" + " or \r\n\t\t" shutil.copy("" + root + "\\" + a, '' + dst + '\\asp\\' + ma1 + '.asp') if a.endswith(".aspx"): ma = sha1sum("" + root + "\\" + a) ma1 = str(ma) if os.path.exists('' + dst + '\\aspx\\' + ma1 + '.aspx'): pass else: print "\t\thash.sha1(0, filesize) == " + "\"" + ma1 + "\"" + " or \r\n\t\t" shutil.copy("" + root + "\\" + a, '' + dst + '\\aspx\\' + ma1 + '.aspx') if a.endswith(".py"): ma = sha1sum("" + root + "\\" + a) ma1 = str(ma) print "\thash.sha1(0, filesize) == " + ma1 + " or \r\n\t" if os.path.exists('' + dst + '\\others\\' + ma1 + '.py'): print "" else: shutil.copy("" + root + "\\" + a, '' + dst + '\\others\\' + ma1 + '.py') if a.endswith(".pl"): ma=sha1sum("" + root + "\\" + a) ma1=str(ma) print "\thash.sha1(0, filesize) == " + ma1 + " or \r\n\t" if os.path.exists('' + dst + '\\others\\' + ma1+'.pl'): print "" else: shutil.copy("" + root + "\\" + a, '' + dst + '\\others\\' + ma1 +'.pl') if a.endswith(".war"): ma = sha1sum("" + root + "\\" + a) ma1 = str(ma) if os.path.exists('' + dst + '\\war\\' + ma1 + '.war'): pass else: print "\t\thash.sha1(0, filesize) == " + "\"" + ma1 + "\"" + " or \r\n\t\t" shutil.copy("" + root + "\\" + a, '' + dst + '\\war\\' + ma1 + '.war') if a.endswith(".jspx"): ma = sha1sum("" + root + "\\" + a) ma1 = str(ma) if os.path.exists('' + dst + '\\jspx\\' + ma1 + '.jspx'): pass else: print "\t\thash.sha1(0, filesize) == " + "\"" + ma1 + "\"" + " or \r\n\t\t" shutil.copy("" + root + "\\" + a, '' + dst + '\\jspx\\' + ma1 + '.jspx') recursive(root, i) if __name__ == '__main__': recursive(root,"")
文件递归 迁移 查找指定类型的文件
最新推荐文章于 2020-01-14 10:08:36 发布