[Python] 纯文本查看 复制代码
import os
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
def rename_files(file_mapping, log_file):
"""
根据文件映射关系重命名文件,并记录日志。
:param file_mapping: 文件名映射字典,键为旧文件名,值为新文件名。
:param log_file: 日志文件路径。
"""
try:
with open(log_file, 'a') as log:
for old_name, new_name in file_mapping.items():
for file in os.listdir():
file_base, file_ext = os.path.splitext(file)
old_base, old_ext = os.path.splitext(old_name)
if file_base == old_base:
new_file = new_name if '.' in new_name else new_name + file_ext
os.rename(file, new_file)
log.write(f"Renamed {file} to {new_file}\n")
print(f"Renamed {file} to {new_file}")
messagebox.showinfo("成功", "文件已成功重命名。")
except Exception as e:
messagebox.showerror("错误", f"错误: {e}")
print(f"Error: {e}")
def delete_substring_from_files(directory, substring, log_file):
"""
从文件名中删除指定的子字符串,并记录日志。
:param directory: 文件所在目录。
:param substring: 要删除的子字符串。
:param log_file: 日志文件路径。
"""
try:
with open(log_file, 'a') as log:
for root, _, files in os.walk(directory):
for file in files:
if substring in file:
new_name = file.replace(substring, "")
old_name = os.path.join(root, file)
new_name = os.path.join(root, new_name)
os.rename(old_name, new_name)
log.write(f"Renamed {old_name} to {new_name}\n")
print(f"Renamed {old_name} to {new_name}")
messagebox.showinfo("成功", "子字符串已成功从文件中删除。")
except Exception as e:
messagebox.showerror("错误", f"错误: {e}")
print(f"Error: {e}")
def change_file_extension(directory, old_ext, new_ext, log_file):
"""
批量修改文件后缀,并记录日志。
:param directory: 文件所在目录。
:param old_ext: 旧的文件后缀。
:param new_ext: 新的文件后缀。
:param log_file: 日志文件路径。
"""
try:
with open(log_file, 'a') as log:
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(old_ext):
base = os.path.splitext(file)[0]
new_name = base + new_ext
old_name = os.path.join(root, file)
new_name = os.path.join(root, new_name)
os.rename(old_name, new_name)
log.write(
f"Changed extension of {old_name} to {new_name}\n")
print(f"Changed extension of {old_name} to {new_name}")
messagebox.showinfo("成功", "文件后缀已成功修改。")
except Exception as e:
messagebox.showerror("错误", f"错误: {e}")
print(f"Error: {e}")
def select_directory():
"""
选择要操作的目录。
"""
directory = filedialog.askdirectory()
if directory:
directory_entry.delete(0, tk.END)
directory_entry.insert(0, directory)
def load_excel():
"""
加载包含文件名映射关系的 Excel 文件。
"""
file_path = filedialog.askopenfilename(
filetypes=[("Excel files", "*.xlsx;*.xls")])
if file_path:
try:
df = pd.read_excel(file_path)
for index, row in df.iterrows():
old_name = str(row[0])
new_name = str(row[1])
file_mapping[old_name] = new_name
mapping_listbox.insert(tk.END, f"{old_name} -> {new_name}")
messagebox.showinfo("成功", "Excel 文件已成功加载。")
except Exception as e:
messagebox.showerror("错误", f"加载 Excel 文件时出错: {e}")
def clear_mappings():
"""
清空文件名映射关系。
"""
file_mapping.clear()
mapping_listbox.delete(0, tk.END)
messagebox.showinfo("成功", "文件名映射关系已清空。")
def start_renaming():
"""
开始重命名文件。
"""
directory = directory_entry.get()
if directory:
os.chdir(directory)
rename_files(file_mapping, 'rename_log.txt')
else:
messagebox.showerror("错误", "请选择一个目录")
def start_deleting():
"""
开始删除文件名中的指定子字符串。
"""
directory = directory_entry.get()
substring = substring_entry.get()
if directory and substring:
delete_substring_from_files(directory, substring, 'rename_log.txt')
else:
messagebox.showerror("错误", "请选择一个目录并输入要删除的子字符串")
def start_changing_extension():
"""
开始批量修改文件后缀。
"""
directory = directory_entry.get()
old_ext = old_ext_entry.get()
new_ext = new_ext_entry.get()
if directory and old_ext and new_ext:
change_file_extension(directory, old_ext, new_ext, 'rename_log.txt')
else:
messagebox.showerror("错误", "请选择一个目录并输入旧后缀和新后缀")
# 初始化文件名映射字典
file_mapping = {}
# 创建主窗口
root = tk.Tk()
root.title("文件重命名器")
# 选择目录
tk.Label(root, text="选择重命名文件目录:").grid(row=0, column=0, padx=10, pady=10)
directory_entry = tk.Entry(root, width=50)
directory_entry.grid(row=0, column=1, padx=10, pady=10)
tk.Button(root, text="浏览", command=select_directory).grid(
row=0, column=2, padx=10, pady=10)
# 加载 Excel 文件
tk.Button(root, text="加载重命名对照 Excel 文件", command=load_excel).grid(
row=1, columnspan=3, pady=10)
# 显示映射关系
mapping_listbox = tk.Listbox(root, width=50)
mapping_listbox.grid(row=2, columnspan=3, padx=10, pady=10)
# 清空映射关系按钮
tk.Button(root, text="清空对照关系", command=clear_mappings).grid(
row=3, columnspan=3, pady=10)
# 删除子字符串
tk.Label(root, text="输入要删除的子字符串:").grid(row=4, column=0, padx=10, pady=10)
substring_entry = tk.Entry(root)
substring_entry.grid(row=4, column=1, padx=10, pady=10)
tk.Button(root, text="删除子字符串", command=start_deleting).grid(
row=4, column=2, padx=10, pady=10)
# 修改文件后缀
tk.Label(root, text="输入旧后缀:(后缀前面加点例如.png)").grid(
row=5, column=0, padx=10, pady=10)
old_ext_entry = tk.Entry(root)
old_ext_entry.grid(row=5, column=1, padx=10, pady=10)
tk.Label(root, text="输入新后缀:(后缀前面加点例如.png)").grid(
row=6, column=0, padx=10, pady=10)
new_ext_entry = tk.Entry(root)
new_ext_entry.grid(row=6, column=1, padx=10, pady=10)
tk.Button(root, text="修改文件后缀", command=start_changing_extension).grid(
row=6, column=2, padx=10, pady=10)
# 重命名按钮
tk.Button(root, text="重命名文件", command=start_renaming).grid(
row=7, columnspan=3, pady=20)
# 运行应用程序
root.mainloop()