欢迎大家一起开发,一起完善
代码我放到下面
(小生不才,写的不好)
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askdirectory
import os
import time
from tkinter import ttk
import re
class GUI(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.creat_window()
self.path = "d:/"
def creat_window(self):
self.f1 = Frame(self)
self.f1.pack()
self.f2 = Frame(self)
self.f2.pack()
self.f3 = Frame(self)
self.f3.pack()
self.f4 = Frame(self)
self.f4.pack()
l1 = Label(self.f1, text="批量更改文件名", bg="pink", fg="white", fon=("宋体", 20))
l1.grid(row=0, column=0, rowspan=2, columnspan=2, sticky=NSEW)
self.l2 = ttk.Entry(self.f2)
self.l2.insert(0, "文件夹路径")
self.l2.grid(row=1, column=0, stick=EW, pady=20)
co = ttk.Button(self.f2, text="选择文件夹", command=self.coll)
co.grid(row=1, column=1, sticky=EW)
self.entry01 = ttk.Entry(self.f2)
# self.entry01.insert(0, "更改后文件名")
self.entry01.grid(row=2, column=0, sticky=NSEW, pady=20)
self.btn = ttk.Button(self.f2, text="更改文件名", command=self.inform)
self.btn.grid(row=2, column=1, sticky=EW)
self.v = StringVar()
self.v.set("0")
self.v1 = ttk.Radiobutton(self.f2, text="前缀名", value="0", variable=self.v)
self.v2 = ttk.Radiobutton(self.f2, text="后缀名", value="1", variable=self.v)
self.v1.grid(row=3, column=0)
self.v2.grid(row=3, column=1)
self.checkbutn()
self.l3 = Text(self.f3, width=36, height=16, bg="lightblue", yscrollcommand=True)
self.l3.insert(INSERT, "终端\n")
scroll = ttk.Scrollbar(self.f3, orient=VERTICAL, takefocus=0.1)
scroll.place(relwidth=0.04, relheight=1, relx=0.9575, rely=0)
scroll.config(command=self.l3.yview)
self.l3.config(yscrollcommand=scroll.set)
self.l3.pack()
self.ra = StringVar()
self.ra.set("0")
self.ra1 = ttk.Radiobutton(self.f2, text="数字排前", value="0", variable=self.ra)
self.ra2 = ttk.Radiobutton(self.f2, text="数字排后", value="1", variable=self.ra)
self.ra1.grid(row=4, column=0)
self.ra2.grid(row=4, column=1)
self.r = StringVar()
self.r.set("1")
self.r1 = ttk.Radiobutton(self.f2, text="使用正则", value="0", variable=self.r)
self.r2 = ttk.Radiobutton(self.f2, text="不使用正则", value="1", variable=self.r)
self.r1.grid(row=5, column=0)
self.r2.grid(row=5, column=1)
signname1 = 47136
signname2 = signname1-17153
sign_p1 = chr(int(signname1/2-1))
sign_p2 = str(chr((signname2)))
signname = sign_p1 + sign_p2
ls = Label(self, text=signname, fon=("黑体", 10), fg="blue")
ls.pack(side="right", padx=1, pady=5)
def checkbutn(self):
butn = ttk.Button(self.f2, text="确定", command=self.justyfilename)
butn.grid(row=6, column=1, sticky=EW)
def justyfilename(self):
part_name = self.entry01.get()
self.l3.insert(INSERT, part_name)
f_name = self.ra.get()
re_name = self.r.get()
lis = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
try:
list_dir = os.listdir(self.path)
if self.v.get() == "1":
for listna in list_dir:
list_name = self.path + listna
b_name = listna.split(".")[0]
# print(b_name)
full_name = self.path + b_name + "." + str(part_name)
# print(full_name)
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
else:
self.l3.insert(INSERT, "前缀名\n")
length = len(list_dir)
i = 0
for listna in list_dir:
list_name = self.path + listna
b_name = listna.split(".")[1]
# print(b_name)
if re_name == "0":
re_part_name = re.findall('.*\[(.*)\].*', part_name)[0]
part_name1 = part_name.split("[")[0]
part_name3 = part_name.split("]")[-1]
part_name2 = part_name.split("[")[1].replace("1-9]", "")
if "1-9" in re_part_name:
# print("jjjjjjjjh")
i += 1
full_name = self.path + part_name1 + str(i) + part_name2 + part_name3 + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif "a-z" in re_part_name:
en_name = lis
i += 1
part_name2 = part_name.split("[")[1].replace("a-z]", "")
full_name = self.path + part_name1 + str(en_name) + part_name3 + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif re_name == "1":
if f_name == "0":
i += 1
line_name = i
full_name = self.path + str(line_name) + str(part_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
else:
i += 1
line_name = i
full_name = self.path + str(part_name) + str(line_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
except Exception:
self.message()
def inform(self):
messagebox.showinfo(title="<注意>", message="直接输入文件名,无需加点")
def message(self):
messagebox.showinfo(title="<注意>", message="请输入文件夹路径,并按顺序完成,或出现了某种未知错误")
def coll(self):
self.path = askdirectory(title="选择文件", initialdir="d:/")
self.path = self.path+"/"
# print(self.path) #要增加判断文件夹还是多个文件
# 判断前缀,加字母数字下划区分
self.l3.insert(INSERT, self.path)
self.l2.insert(0, self.path)
if __name__ == '__main__':
root = Tk()
root.geometry("280x490+200+200")
root.title("批量更改文件名")
root.iconbitmap("b.ico")
root.resizable(width=False, height=False)
app = GUI(master=root)
root.mainloop()