N120_远程勘验_计算文件夹下文件的md5

#!usr/bin/env python 
#encoding:utf-8
import os,sys
import time
import hashlib
import pandas as pd
from time import timezone
data = pd.DataFrame(columns=["文件名","路径","md5","文件大小","创建时间","修改时间","访问时间"])
print(data.head())
########################################
def Get_Md5_Of_File(filename):
        if not os.path.isfile(filename):
                return
        myhash = hashlib.md5()
        f = open(filename,'rb')
        while True:
                b = f.read(80960000)
                if not b :
                        break
                myhash.update(b)
        f.close()
        return myhash.hexdigest()
########################################
jieguo = "165 远程勘验 计算文件夹下文件的md5.txt"
def Get_Md5_Of_Folder(dir):
    if os.path.exists(jieguo):
        os.remove(jieguo)     
        print("删除成功")
    MD5File = jieguo
#     outfile = open(MD5File,'w')
    for root, subdirs, files in os.walk(dir):
        for index,file in enumerate(files):
            filefullpath = os.path.join(root,file)
            filerelpath = os.path.relpath(filefullpath,dir)
            md5 = Get_Md5_Of_File(filefullpath)
            fsize = os.path.getsize(filefullpath) #以字节为单位获取文件大小
            chuan_time = time.localtime(os.path.getctime(filefullpath)) #获取文件创建时间
            xiugai_time = time.localtime(os.path.getmtime(filefullpath)) #最后修改时间
            fang_time = time.localtime(os.path.getatime(filefullpath)) #最后访问时间
            data.loc[index,"文件名"] = file
            data.loc[index,"路径"] = filefullpath
            data.loc[index,"md5"] = md5
            data.loc[index,"文件大小"] = fsize  
            data.loc[index,"创建时间"] = time.strftime('%Y年%m月%d日 %H时%M分%S秒',chuan_time)  
            data.loc[index,"修改时间"] = time.strftime('%Y年%m月%d日 %H时%M分%S秒',xiugai_time)
            data.loc[index,"访问时间"] = time.strftime('%Y年%m月%d日 %H时%M分%S秒',fang_time)
    print(data.head(1))
    data.to_excel("165 远程勘验 计算文件夹下文件的md5.xlsx",index=0)
Get_Md5_Of_Folder(r"C:\Users\wj\Desktop\11")
Empty DataFrame
Columns: [文件名, 路径, md5, 文件大小, 创建时间, 修改时间, 访问时间]
Index: []
        文件名                               路径  ...                   修改时间                   访问时间
0  处理后.docx  C:\Users\wj\Desktop\11\处理后.docx  ...  2022年10月22日 21时48分14秒  2022年10月23日 20时02分05秒

[1 rows x 7 columns]







#!usr/bin/env python 
#encoding:utf-8
import os,sys
import hashlib
from tkinter import *
import tkinter.filedialog
########################################
def Get_Md5_Of_File(filename):
        if not os.path.isfile(filename):
                return
        myhash = hashlib.md5()
        f = open(filename,'rb')
        while True:
                b = f.read(80960000)
                if not b :
                        break
                myhash.update(b)
        f.close()
        return myhash.hexdigest()
########################################
def Get_Md5_Of_Folder(dir):
    if os.path.exists("md5zhijisuantmp.txt"):
        os.remove("md5zhijisuantmp.txt")     
        print("删除成功")
    MD5File = "md5zhijisuantmp.txt"
    outfile = open(MD5File,'w')
    for root, subdirs, files in os.walk(dir):
        for file in files:
            filefullpath = os.path.join(root,file)
            filerelpath = os.path.relpath(filefullpath,dir)
            md5 = Get_Md5_Of_File(filefullpath)
            fsize = os.path.getsize(filefullpath)
            # outfile.write('%40s' %str(file)+"    ~    "+'%40s' %md5+"    ~    "+'%20s' %str(fsize)+"字节"+'\n')
            outfile.write(str(file)+"@"+md5+"@"+str(fsize)+"字节"+'\n')            
    outfile.close()
    return Get_Md5_Of_File(MD5File)
#####弹出对话框并选中一个你要计算的文件,会将整个文件夹的文件都计算MD5
root = Tk()
root.title("自动计算MD5")     
root.geometry('500x100')                                 #是x 不是*     
root.resizable(width=True, height=True)        #宽不可变, 高可变,默认为True
def xz():
        xuanzhefilename = tkinter.filedialog.askopenfilename()
        if xuanzhefilename != '':
                lb.config(text = "您选择的文件是:"+xuanzhefilename);
                parent_path = os.path.dirname(xuanzhefilename)
                
                

                Get_Md5_Of_Folder(parent_path)
                print("计算完成")

        else:
        	lb.config(text = "您没有选择任何文件");
lb = Label(root,text = '')
lb.pack() #包裹; 一群; (纸牌的) 一副; 一组
btn = Button(root,text="弹出选择文件对话框",command=xz)
btn.pack()
root.mainloop()    #循环执行,没有最后一句执行不了
####解决了远程勘验后计算MD5值总是要查看属性复制文件大小,还容易搞错的问题
####下一步精简下,并自动打开文件,再下一步,弄个中间表,报告自动化

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kafeihule

感谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值