import hashlib import os import sys import tkinter.messagebox as messagebox
defget_file_md5(file_path): """计算文件的MD5值""" md5_hash = hashlib.md5() try: withopen(file_path, 'rb') as f: while chunk := f.read(8192): md5_hash.update(chunk) return md5_hash.hexdigest() except Exception as e: messagebox.showerror("Error", f"Error reading file {file_path}: {e}") returnNone
defrename_file(file_path): """将文件重命名为其MD5值""" md5_value = get_file_md5(file_path) if md5_value: new_name = os.path.join(os.path.dirname(file_path), md5_value + os.path.splitext(file_path)[1]) try: os.rename(file_path, new_name) except Exception as e: messagebox.showerror("Error", f"Error renaming file {file_path}: {e}") else: messagebox.showwarning("Warning", f"Could not compute MD5 for {file_path}")
defmain(): iflen(sys.argv) < 2: messagebox.showwarning("Warning", "No files provided!") return
for file_path in sys.argv[1:]: file_path = file_path.strip('"') if os.path.isfile(file_path): rename_file(file_path) else: messagebox.showwarning("Warning", f"{file_path} is not a valid file")