文件递归 迁移 查找指定类型的文件

# -*- 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,"")






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值