下面是代码:
[Python] 纯文本查看 复制代码import os
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
folder_path = ""
def select_folder():
global folder_path
folder_path = filedialog.askdirectory()
folder_label.config(text=folder_path)
return folder_path
def input_chars():
chars_to_delete = delete_entry.get()
if chars_to_delete:
root.after(100, rename_files, chars_to_delete)
else:
messagebox.showwarning("警告", "请输入要删除的字符!")
def rename_files(chars_to_delete):
global folder_path
if folder_path:
for root, dirs, files in os.walk(folder_path):
for file in files:
if chars_to_delete in file:
# 构建新的文件名
new_filename = file.replace(chars_to_delete, '')
# 获取旧文件名和新的文件名的完整路径
old_file_path = os.path.join(root, file)
new_file_path = os.path.join(root, new_filename)
# 重命名文件
os.rename(old_file_path, new_file_path)
messagebox.showinfo("完成", "所有文件名中包含指定字符的文件都已重命名。")
else:
messagebox.showwarning("警告", "请选择文件夹!")
root = tk.Tk()
root.title("批量重命名文件名中字符")
folder_label = tk.Label(root, text="请选择文件夹:")
folder_label.pack()
folder_button = tk.Button(root, text="选择文件夹", command=select_folder)
folder_button.pack()
delete_label = tk.Label(root, text="请输入要删除的字符:")
delete_label.pack()
delete_entry = tk.Entry(root)
delete_entry.pack()
input_button = tk.Button(root, text="重命名", command=input_chars)
input_button.pack()
root.mainloop()
相同的字符可以处理了,如果是下面方框中的,没规律的数字,但是又不相同,怎么办?
下面是另一段代码:
[Python] 纯文本查看 复制代码import tkinter as tk
from tkinter import filedialog
import os
import re
def select_folder():
folder_path = filedialog.askdirectory()
entry_folder.delete(0, tk.END)
entry_folder.insert(0, folder_path)
def search_and_remove():
folder_path = entry_folder.get()
if not os.path.exists(folder_path):
tk.messagebox.showerror("错误", "文件夹不存在")
return
files = os.listdir(folder_path)
for file in files:
if re.search(r'\[[^]]*\]', file):
# 构建新的文件名
new_filename = re.sub(r'\[.*?\]', '', file)
# 重命名文件
os.rename(os.path.join(folder_path, file), os.path.join(folder_path, new_filename))
print(f"已成功处理文件: {file} -> {new_filename}")
root = tk.Tk()
root.title("搜索并删除含有[]的文件名")
label_folder = tk.Label(root, text="请选择文件夹:")
label_folder.pack()
entry_folder = tk.Entry(root)
entry_folder.pack()
button_folder = tk.Button(root, text="选择文件夹", command=select_folder)
button_folder.pack()
button_search = tk.Button(root, text="搜索并删除", command=search_and_remove)
button_search.pack()
root.mainloop()
1719880048090.png (139.65 KB, 下载次数: 0)
下载附件
2024-7-2 08:31 上传
程序运行界面:
微信图片_20240702083325.png (10.64 KB, 下载次数: 0)
下载附件
2024-7-2 08:33 上传
微信图片_20240702083244.png (9.68 KB, 下载次数: 0)
下载附件
2024-7-2 08:33 上传
打包后的可执行程序:
链接:https://pan.baidu.com/s/1CX51trcOIAJj87WCKCEpew
提取码:52pj
[color=]有个坛友@@SU150228 讲的很有道理,有矛就有盾,功能上应该有文件名中增加的功能,现在修改了下,可以增加,也可以删除,代码如下,修改后的下载链接在最后面
:
[Python] 纯文本查看 复制代码import os
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
folder_path = ""
chars_to_delete = "" # 要删除的字符
chars_to_add = "" # 要添加的字符
def select_folder():
global folder_path
folder_path = filedialog.askdirectory()
folder_label.config(text=folder_path)
def input_chars_to_delete():
global chars_to_delete
chars_to_delete = delete_entry.get()
if chars_to_delete:
rename_files(chars_to_delete)
else:
messagebox.showwarning("警告", "请输入要删除的字符!")
def input_chars_to_add():
global chars_to_add
chars_to_add = add_entry.get()
if chars_to_add:
rename_files(chars_to_add, add=True)
else:
messagebox.showwarning("警告", "请输入要添加的字符!")
def rename_files(chars_to_add, add=False):
global folder_path
if folder_path:
for root, dirs, files in os.walk(folder_path):
for file in files:
if add:
new_filename = chars_to_add + file
else:
new_filename = file.replace(chars_to_add, '')
# 获取旧文件名和新的文件名的完整路径
old_file_path = os.path.join(root, file)
new_file_path = os.path.join(root, new_filename)
# 重命名文件
try:
os.rename(old_file_path, new_file_path)
except FileNotFoundError: # 如果文件被其他程序打开或移动,可能会引发错误
pass
messagebox.showinfo("完成", "所有文件名中包含指定字符的文件都已处理。")
else:
messagebox.showwarning("警告", "请选择文件夹!")
root = tk.Tk()
root.title("批量重命名文件名中字符")
folder_label = tk.Label(root, text="请选择文件夹:")
folder_label.pack()
folder_button = tk.Button(root, text="选择文件夹", command=select_folder)
folder_button.pack()
delete_label = tk.Label(root, text="请输入要删除的字符:")
delete_label.pack()
delete_entry = tk.Entry(root)
delete_entry.pack()
delete_button = tk.Button(root, text="删除字符", command=input_chars_to_delete)
delete_button.pack()
add_label = tk.Label(root, text="请输入要添加的字符:")
add_label.pack()
add_entry = tk.Entry(root)
add_entry.pack()
add_button = tk.Button(root, text="添加字符", command=input_chars_to_add)
add_button.pack()
root.mainloop()
11d8d24bb19cb56e5ed7a776c4679d9.png (12.57 KB, 下载次数: 0)
下载附件
2024-7-2 11:15 上传
应坛友要求,修改后的打包后的程序下载:
链接:https://pan.baidu.com/s/14gF-ar1L12g2UTMal9cNaw
提取码:52pj