diff --git a/getFiles.py b/getFiles.py index aeeec84..5d19bf8 100644 --- a/getFiles.py +++ b/getFiles.py @@ -11,6 +11,27 @@ def get_file_list(_path): return file_list +def get_empty_folder_list(path): + folder_list = [] + for f in os.walk(path): + if len(f[2]) == 0 and len(f[1]) == 0: + folder_list.append(f[0]) + return folder_list + + +def remove_empty_folder(path): + del_folder_list = [] + del_folder_list.extend(get_empty_folder_list(path)) + + while len(del_folder_list) > 0: + for f in del_folder_list: + if os.path.exists(f): + print(f"删除空文件夹 {f}") + os.rmdir(f) + del_folder_list = [] + del_folder_list.extend(get_empty_folder_list(path)) + + if __name__ == "__main__": result = get_file_list(r"E:\Backup\Tools\Normal\浏览器") for r in result: diff --git a/main.py b/main.py index e48dc0a..1d018d4 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,20 @@ -from getFiles import get_file_list +import os +import win32con +import win32api +from getFiles import get_file_list, remove_empty_folder from getMd5 import get_file_md5 from db import * from time import time as t +disk = ("Data_sjry" + "" + "" + "") +cate = "sjry" +path = r"F:\Temp\sjry\2023-10" -def main_process(): - disk = "Myxx_Backup_16T_3" - cate = "leshe" - path = r"X:\leshe" +def main_process(): _exist_file_list = [] if disk == "" or cate == "" or path == "": @@ -32,10 +38,10 @@ def main_process(): file_list_len = len(unchecked_list) for (i, f) in enumerate(unchecked_list): - print(f"({i+1}/{file_list_len}) - 开始处理: {f} - {t()}") + print(f"({i + 1}/{file_list_len}) - 开始处理: {f} - {t()}") t_start = t() md5 = get_file_md5(f) - print(f"完成MD5计算耗时:{t()-t_start}") + print(f"完成MD5计算耗时:{t() - t_start}") if exist := is_exist(md5): _exist_file_list.append(f) print(f"重复文件!!!md5为({md5})的文件已经存在,文件路径:{exist['path']} -> {f}") @@ -45,13 +51,20 @@ def main_process(): return _exist_file_list -if __name__ == "__main__": - exist_file_list = main_process() - if len(exist_file_list) > 0: +def handle_exist(_exist_file_list): + if len(_exist_file_list) > 0: with open('dup.txt', 'w', encoding='utf-8') as dup_file: - for ef in exist_file_list: - print(ef) - dup_file.write(ef) + for ef in _exist_file_list: + dup_file.write(ef + "\r\n") + win32api.SetFileAttributes(ef, win32con.FILE_ATTRIBUTE_NORMAL) + os.remove(ef) + print(f"删除: {ef}") dup_file.close() else: print("没有找到重复文件") + + +if __name__ == "__main__": + exist_file_list = main_process() + handle_exist(exist_file_list) + remove_empty_folder(path)