import os,sys
import time
import hashlib
import pandas as pd
from time import timezone
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):
data = pd.DataFrame(columns=["文件名","路径","md5","文件大小","创建时间","修改时间","访问时间"])
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("well done")
return data
df1= Get_Md5_Of_Folder(r"C:\Users\wj\Desktop\11")
print(df1.shape)
df2= Get_Md5_Of_Folder(r"C:\Users\wj\Desktop\22")
print(df2.shape)
df_zong = pd.merge(df1, df2, how = "outer",left_on = "md5", right_on = "md5",
sort = True, suffixes = ("_1","_2"))
jieguo = "164 用md5比较两个文件夹文件的区别(看看是不是文件被动).xlsx"
df_zong[["md5","文件名_1","文件名_2","路径_1","路径_2","文件大小_1","文件大小_2","创建时间_1","创建时间_2","修改时间_1","修改时间_2","访问时间_1","访问时间_2"]].to_excel(jieguo,index=0)
well done
(5, 7)
well done
(6, 7)