python MD5

代碼類型:python
功能說明:獲取文件MD5,及最後修改内容時間


import subprocess
import time
import os
class getmd5(object):
    def get_MD5(self, fun,batpath,logpath):
        print("正在獲取MD5,請稍後...")
        print("開始遍歷目錄下的bat程式")
        list = os.listdir(batpath)
        with open("%s\%s.txt" % (logpath, fun), "a")as f:
            print("開始計算程式MD5....")
            for i in list:
                if i.split(".")[-1] == "bat" or i.endswith(".BAT") or i.endswith(".cmd") or i.endswith(".ini"):
                    batname = os.path.join(batpath, i)
                    lasttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.stat(batname).st_mtime))
                    pp = 'Certutil -hashfile %s MD5 | find /i /v "MD5" |find /i /v "Certutil"' % batname
                    mm = subprocess.getoutput("%s" % pp)
                    f.write(batname + "," + mm + "," + lasttime + "\n")
            print("MD5計算完成")
        f.close()

-------------------------- 此处为getmd5 的总程式  --------------------------

import os
import sys
from chkmd5 import check
from getmd5 import getmd5
class path(object):
    def __init__(self):
        self.batpath = r"E:\old\F1ABBA"
        self.logpath = r"E:\old\temp"
    def mklog(self,i):
        # 刪除log文件夾
        if not os.path.exists(self.logpath):
            os.mkdir(self.logpath)
        # shutil.rmtree(self.logpath)
        log = os.path.join(self.logpath, i)+".txt"
        if os.path.exists(log):
            os.remove(log)
        if i == "getmd5":
            getmd5().get_MD5("getmd5",self.batpath,self.logpath)
            print("get md5 成功!")
        elif i=="chkmd5":
            check().check_Md5("chkmd5",self.batpath,self.logpath)
            print("check  md5 成功!")
        else:
            self.run()
    def run(self,argv):
        self.mklog(argv[0])
if __name__ == '__main__':
    run=path()
    run.run(sys.argv[1:])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值