利用python删除群晖重复文件(缓存文件MD5方式)
原文每次扫描都需要重新计算MD5,对于大文件来说,磁盘消耗较大,增加了缓存文件存储md5,每次扫描只计算新文件,提高效率。
修改增加比较修改时间和大小,防止文件修改后不更新md5值:
不废话,直接贴代码:
import os
import hashlib
# 只删除以下列表中的重复文件类型.如果想删除其他类型的文件,自己添加一下就行了
file_type = ['.jpg', '.jpeg', '.png', '.gif', '.psd', '.bmp', '.webp', '.mp4', '.mkv', '.avi', '.mov', 'mpeg', 'mpg',
'.rar', '.zip']
check_files = []
#自行修改目录列表
work_dir_list = [r'/volume2/111', r'/volume1/222']
class fileMd5:
def __init__(self, md5: str, st_mtime: str, len: int):
self.md5 = md5
self.st_mtime = st_mtime
self.len = len
def save_md5_file(files_dict: dict):
if files_dict is None:
return
try:
with open(md5_txt, "w") as f:
for path_md5, file_md5, in files_dict.items():
f.write(str(path_md5) + "=" + str(file_md5.md5) + ','
+ str(file_md5.st_mtime) + ','
+ str(file_md5.len) + 'n')
except Exception as e:
print(e)
pass
def open_md5_file():
files_md5 = {}
try:
with open(md5_txt, "r") as f:
for md5_line in iter(lambda: f.readline(), ""):
list_line = md5_line.split('=')
if len(list_line) != 2:
continue
list_md5 = list_line[1].split(',')
if len(list_md5) != 3:
continue
md5 = fileMd5(list_md5[0].strip(), list_md5[1].strip(), int(list_md5[2]))
files_md5[list_line[0].strip()] = md5
except Exception as e:
print(e)
pass
return files_md5
def remove_repeat_files():
for work_dir in work_dir_list:
for root, dirs, files in os.walk(work_dir):
for name in files:
p_type = os.path.splitext(os.path.join(root, name))[1]
if p_type in file_type:
check_files.append(os.path.join(root, name))
for name in dirs:
p_type = os.path.splitext(os.path.join(root, name))[1]
if p_type in file_type:
check_files.append(os.path.join(root, name))
files_dict = {}
new_files_md5 = {}
files_md5 = open_md5_file()
r_index = 0
print('Files Num:%s' % len(check_files))
for file_path in check_files:
try:
md5_path = hashlib.md5()
md5_path.update(file_path.encode('utf-8'))
path_md5 = md5_path.hexdigest()
file_md5 = files_md5.get(path_md5)
file_stats = os.stat(file_path)
st_mtime = file_stats.st_mtime
file_len = file_stats.st_size
if file_md5 is None or file_md5.st_mtime != str(st_mtime) or file_len != file_md5.len:
md5_hash = hashlib.md5()
with open(file_path, "rb+") as f:
for byte_block in iter(lambda: f.read(4096), b""):
md5_hash.update(byte_block)
file_md5 = fileMd5(str(md5_hash.hexdigest()), str(st_mtime), file_len)
print('Check file MD5:%s' % file_path)
files_md5[path_md5] = file_md5
new_files_md5[path_md5] = file_md5
if files_dict.get(file_md5.md5) is None:
files_dict[file_md5.md5] = file_path
else:
d_path = files_dict[file_md5.md5]
d_path_stats = os.stat(d_path)
d_time = d_path_stats.st_ctime
f_time = file_stats.st_ctime
if d_time > f_time:
os.remove(d_path)
files_dict[file_md5.md5] = file_path
print('Delete File:', d_path)
r_index += 1
else:
os.remove(file_path)
print('Delete File:', file_path)
r_index += 1
except Exception as e:
print(e)
pass
print('File Count:%s, Repeat Files Num:%s. All deleted!' %( len(check_files),str(r_index)))
save_md5_file(new_files_md5)
if __name__ == '__main__':
remove_repeat_files()
可以在ssh或者任务计划里执行
作者声明本文无利益相关,欢迎值友理性交流,和谐讨论~
cbs0660
校验提示文案
adobe1
校验提示文案
稀碎的银子
校验提示文案
雨鱼1
name 'md5_txt' is not defined” 说md5_txt没有定义,是哪里的问题呢
校验提示文案
值友6207679554
个人测试没问题,即使有问题了,根据文件名大不了重新下载呗。
校验提示文案
值友8865054275
校验提示文案
ozzzo
校验提示文案
大盘鸡拌面
校验提示文案
Jack_Zeng
校验提示文案
是你大叔呀
校验提示文案
小海盗
校验提示文案
非洲小野马
校验提示文案
todaya
校验提示文案
todaya
校验提示文案
ozzzo
校验提示文案
值友8865054275
校验提示文案
非洲小野马
校验提示文案
值友6207679554
个人测试没问题,即使有问题了,根据文件名大不了重新下载呗。
校验提示文案
雨鱼1
name 'md5_txt' is not defined” 说md5_txt没有定义,是哪里的问题呢
校验提示文案
小海盗
校验提示文案
稀碎的银子
校验提示文案
adobe1
校验提示文案
cbs0660
校验提示文案
是你大叔呀
校验提示文案
Jack_Zeng
校验提示文案
大盘鸡拌面
校验提示文案