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
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]
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(str(file)+"@"+md5+"@"+str(fsize)+"字节"+'\n')
outfile.close()
return Get_Md5_Of_File(MD5File)
root = Tk()
root.title("自动计算MD5")
root.geometry('500x100')
root.resizable(width=True, height=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()