本帖最后由 不负韶华 于 2020-6-19 21:38 编辑
用python tkinter
写了一个批量修改文件名的小程序,
程序开源,
v1.0版本
欢迎大家一起开发,一起完善
程序,
代码我放到下面
(小生不才,写的不好)
v1.0懒昼网盘:https://devilwst.lanzouj.com/ijHGMdpkosf
*******************************************************************************************************************************************
v2.0版本
继前几天写了批量修改文件名小工具的第一版,这几天紧追慢赶写bug,
代码加入了注释
先说说升级的地方吧:
1.增加了多个文件选择的功能,并不需要一定选择文件夹,
2.增加了保留源文件名功能,可以在源文件名基础上加数字字母,
3.将多个按钮合并为一个确定按。改了很多bug,
3.增加了标准数字功能即可以使用001,002,003,004
来进行命名,关于软件的正则还是很鸡肋,感觉无从下手,下一个版本考虑增加撤回功能,改进正则
代码开源,欢迎大家一起完善,可能还有很多bug
蓝揍云:https://devilwst.lanzouj.com/b00noj9ta 密码:6s6j
代码放到下面(相对于1.0版,代码量翻了两倍)
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.f3 = Frame(self)
self.f3.pack(side="right")
self.f2 = Frame(self)
self.f2.pack()
self.fintenger = Frame(self.f2)
self.fintenger.grid(row=5, column=0, columnspan=2)
self.finside = Frame(self.f2)
self.finside.grid(row=2, column=0)
self.f4 = Frame(self.f2)
self.f4.grid(row=7, column=0)
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)
butn2 = ttk.Button(self.f2, text="选择多个文件", command=self.choose_files)
butn2.grid(row=2, column=1)
self.b = StringVar()
self.b.set("0")
self.b1 = ttk.Radiobutton(self.finside, text="文件夹", value="0", variable=self.b)
self.b2 = ttk.Radiobutton(self.finside, text="文件", value="1", variable=self.b)
self.b1.grid(row=2, column=0)
self.b2.grid(row=2, column=1)
# butn3 = ttk.Button(self.finside, text="ensure", command=self.check_file_type)
# butn3.grid(row=2,column=2)
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=3, column=0, sticky=NSEW, pady=20)
self.btn = ttk.Button(self.f2, text="更改文件名", command=self.inform)
self.btn.grid(row=3, 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=4, column=0)
self.v2.grid(row=4, column=1)
# self.checkbutn()
self.l3 = Text(self.f3, width=50, height=19, bg="lightblue", yscrollcommand=True)
self.l3.insert(INSERT, "www.52pojie.cn\n\n终端\n\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(padx=25, pady=0)
self.ra = StringVar()
self.ra.set("0")
self.ra3 = ttk.Radiobutton(self.fintenger, text="标准数字", value="2", variable=self.ra)
self.ra1 = ttk.Radiobutton(self.fintenger, text="数字排前", value="0", variable=self.ra)
self.ra2 = ttk.Radiobutton(self.fintenger, text="数字排后", value="1", variable=self.ra)
self.ra3.pack(side="left")
self.ra1.pack(side="left")
self.ra2.pack(side="left")
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=6, column=0)
self.r2.grid(row=6, column=1)
signname1 = 47136
signname2 = signname1-17153
sign_p1 = chr(int(signname1/2-1))
sign_p2 = str(chr((signname2)))
signname = "www.52pojie.cn "+sign_p1 + sign_p2
ls = Label(self, text=signname, fon=("黑体", 10), fg="blue")
ls.pack(side="right", padx=1, pady=5)
self.e = StringVar()
self.e.set("1")
self.e1 = ttk.Radiobutton(self.f4, text="保留文件名", value="0", variable=self.e)
self.e2 = ttk.Radiobutton(self.f4, text="取消", value="1", variable=self.e)
self.e1.pack(side="left")
self.e2.pack(side="left")
# butn4 = ttk.Button(self.f4, text="确定保留", command=self.reserve_old_filename)
# butn4.pack(side="left")
butn_help = ttk.Button(self.f2, text="help", command=self.help)
butn_help.grid(row=7, column=1)
butn = ttk.Button(self.f2, text="确定", command=self.justyfilename)
butn.grid(row=8, column=1, sticky=EW)
def reserve_old_filename(self):
"""保留源原文件名并加现在想要的格式"""
reserve_information = self.e.get()
if reserve_information == "0":
part_name = self.entry01.get()
self.l3.insert(INSERT, part_name)
f_name = self.ra.get()
"""数字排前拍后"""
"""使用正则不使用正则"""
try:
list_dir = os.listdir(self.path)
self.l3.insert(INSERT, "前缀名\n")
i = 0
for listna in list_dir:
list_name = self.path + listna
b_name = listna.split(".")[-1]
origion_name = listna.split(".")[0]
if f_name == "0":
"""数字排前"""
i += 1
line_name = i
"""计数"""
full_name = self.path + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif f_name == "1":
"""数字排后"""
i += 1
line_name = i
full_name = self.path + str(origion_name) + str(line_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif f_name == "2":
# print("*" * 60)
i += 1
line_name = i
if i < 10:
full_name = self.path + "00" + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif 100 > i >= 10:
full_name = self.path + "0" + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif i >= 100:
full_name = self.path + str(line_name) + str(origion_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 choose_files(self):
"""选中多个文件"""
try:
self.path_origion = filedialog.askopenfilenames(title="打开多个文件")
base_name = self.path_origion[0]
i = 0
for each_path in self.path_origion:
self.l3.insert(INSERT, each_path+"\n")
# print(each_path)
i += 1
self.l2.insert(0, each_path)
self.l3.insert(INSERT, "选中%s个文件" % i+"\n")
filename = self.path_origion[0].split("/")[-1]
self.path = base_name.replace(filename, "")
self.l3.insert(INSERT, "文件路径为{}".format(self.path) + "\n")
"""****************************************************************
主要功能self.check_file_type()以及self.justifyfilename()
****************************************************************
"""
except Exception:
self.choose_filetree()
# print("发生错误了,an error ocured")
# raise
def help(self):
"""选用文件夹 确定按键"""
information = """\n <帮助>\n\n------------------------------------------------\n1. 选择文件夹或者选择多个文件后,记得把单选框打勾。\n\n2. 标准数字 单选框是一位坛友评论的有些软件只能识别001,002,010,020这样的文件名,该选项就是这个意思。\n\n3. 保留文件名的意思是在源文件名基础之上加数字或者字母。\n\n4. 软件的正则我自己承认,很鸡肋,但又不知道如何下手,希望有兴趣的坛友帮忙改进一下算法啥的。\n\n5. 打算在3.0版本改进关于正则的算法。\n\n6. v2.0版相对v1.0版本改版很大,无论从一些简单的算法还是界面,以及错误处理都有很大改观。\n\n7. 由于使用的是tkinter写的gui,比较丑,pyqt5还没有学,或许在之后的版本改进吧。\n\n8. 可能tkinter有bug,输入框或者text文本框删除文本后会有一个小方框\n\n------------------------------------------------"""
messagebox.showinfo(message=information)
self.l3.insert(INSERT, information)
def justyfilename(self):
"""选择文件夹调用函数"""
reserve_information = self.e.get()
value = self.b.get()
if value == "0":
"""选择文件夹"""
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"]
reserve_information = self.e.get()
if reserve_information == "0":
part_name = self.entry01.get()
self.l3.insert(INSERT, part_name)
f_name = self.ra.get()
"""数字排前拍后"""
"""使用正则不使用正则"""
try:
list_dir = os.listdir(self.path)
self.l3.insert(INSERT, "前缀名\n")
i = 0
for listna in list_dir:
list_name = self.path + listna
b_name = listna.split(".")[-1]
origion_name = listna.split(".")[0]
if f_name == "0":
"""数字排前"""
i += 1
line_name = i
"""计数"""
full_name = self.path + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif f_name == "1":
"""数字排后"""
i += 1
line_name = i
full_name = self.path + str(origion_name) + str(line_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif f_name == "2":
i += 1
line_name = i
if i < 10:
full_name = self.path + "00" + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif 100 > i >= 10:
full_name = self.path + "0" + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif i >= 100:
full_name = self.path + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
except Exception:
self.reserve_old_filenametree()
else:
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]
full_name = self.path + b_name + "." + str(part_name)
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
else:
self.l3.insert(INSERT, "前缀名\n")
"""判断前缀名"""
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:
"""[1-9]判断"""
# 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:
""""[a-z]判断"""
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.no_reserve_old_filenames()
else:
"""选择单个文件
保留原文件名"""
if reserve_information == "0":
part_name = self.entry01.get()
self.l3.insert(INSERT, part_name)
f_name = self.ra.get()
"""数字排前拍后"""
"""使用正则不使用正则"""
list_dir = self.path_origion
self.l3.insert(INSERT, "前缀名\n")
i = 0
for listna in list_dir:
listna = listna.replace(self.path, "")
list_name = self.path + listna
b_name = listna.split(".")[-1]
origion_name = listna.split(".")[0]
try:
if f_name == "0":
"""数字排前"""
i += 1
line_name = i
"""计数"""
full_name = self.path + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif f_name == "1":
"""数字排后"""
i += 1
line_name = i
full_name = self.path + str(origion_name) + str(line_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif f_name == "2":
i += 1
line_name = i
if i < 10:
full_name = self.path + "00" + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif 100 > i >= 10:
full_name = self.path + "0" + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif i >= 100:
full_name = self.path + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
except Exception:
self.reserve_filenames()
# raise
else:
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"]
list_dir = self.path_origion
try:
if self.v.get() == "1":
"""判断后缀名"""
for listna in list_dir:
listna = listna.replace(self.path, "")
b_name = listna.split(".")[-1]
list_name = self.path + listna
full_name = self.path + b_name + "." + str(part_name)
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
else:
self.l3.insert(INSERT, "前缀名\n")
"""判断前缀名"""
i = 0
for listna in list_dir:
listna = listna.replace(self.path, "")
list_name = self.path + listna
b_name = listna.split(".")[-1]
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:
"""[1-9]判断"""
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:
""""[a-z]判断"""
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))
elif f_name == "1":
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))
elif f_name == "2":
# print("*" * 60)
i += 1
line_name = i
if i < 10:
full_name = self.path + "00" + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif 100 > i >= 10:
full_name = self.path + "0" + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
elif i >= 100:
full_name = self.path + str(line_name) + str(origion_name) + "." + b_name
os.rename(list_name, full_name)
self.l3.insert(INSERT, "文件%s重命名成功,新的文件名为%s\n" % (list_name, full_name))
except Exception:
self.mess_manyfiles()
# raise
def mess_manyfiles(self):
messagebox.showinfo(title="<justyfilename函数报错>", message="justyfilename 选择多个文件函数报错,370行之后代码,")
def choose_filetree(self):
messagebox.showinfo(title="<choose_files函数报错>", message="choose_files 选择多个文件函数报错,165行之后代码,")
def reserve_filenames(self):
messagebox.showinfo(title="<justyfilename函数报错>", message="justyfilename 选择多个文件保留源文件名函数报错,320行之后代码,")
def no_reserve_old_filenames(self):
messagebox.showinfo(title="<justyfilename函数报错>", message="justyfilename 选择文件夹不保留源文件名函数报错,264行之后代码,")
def reserve_old_filenametree(self):
messagebox.showinfo(title="<justyfilename函数报错>", message="justyfilename 选择文件夹不保留源文件名函数报错,222行之后代码,")
def reserve_old_filename_error(self):
messagebox.showinfo(title="<reserve_old_filename函数报错>", message="jreserve_old_filename 保留源文件名函数报错,112行之后代码,")
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("730x340+200+200")
root.title("批量更改文件名v2.0")
# root.iconbitmap("b.ico")
root.resizable(width=False, height=False)
app = GUI(master=root)
root.wm_attributes("-alpha", 0.86) # 透明度(0.0~1.0)
root.wm_attributes("-toolwindow", True) # 置为工具窗口(没有最大最小按钮)
root.wm_attributes("-topmost", True) # 永远处于顶层
# root.overrideredirect(True)
root.mainloop()
|