用Python编写过批量修改文件名的脚本程序,代码很简单,运行也比较快,唯一美中不足之处是每次批量修改文件名时都需要执行以下步骤:
为了便于操作,最好还是弄成GUI界面,手动选择文件夹,这样程序也更通用。Python中的GUI库很多,绝大部分都支持跨平台,其中安装python时自带的GUI库是tkinter,本文就学习并创建基于tkinte的批量修改文件名程序。
本文涉及的知识点包括以下几个:
全部代码如下所示:
# coding=gbk import tkinter as tk import os from tkinter.filedialog import askdirectory def BrowseDri(): txtDirPath.set(askdirectory()) def BatchReplaceFileName(): path = txtDirPath.get() strSign=txtRemovedContent.get() files=os.listdir(path) for onefile in files: if onefile.find(strSign)<0: continue oldname=path+"\\"+onefile newname=path+"\\"+onefile.replace(strSign,"") os.rename(oldname,newname) print(oldname,"====>",newname) window=tk.Tk() window.title('批量处理文件名') window.geometry('600x400') tk.Label(window,text='选择文件夹').grid(row=0,column=0) txtDirPath=tk.StringVar() tk.Entry(window,textvariable=txtDirPath).grid(row=0,column=1) tk.Button(window,text='浏览',command=BrowseDri).grid(row=0,column=2) tk.Label(window,text='输入要移除的内容:').grid(row=1,column=0) txtRemovedContent=tk.StringVar() tk.Entry(window,textvariable=txtRemovedContent).grid(row=1,column=1) tk.Button(window,text='移除',command=BatchReplaceFileName).grid(row=1,column=2) tk.mainloop()
最后是程序运行效果,如下面几张截图所示:运行程序后,首先选择要批量处理的文件夹,然后设置文件名中要移除的内容,最后点击移除按钮批量处理文件名。
上文主要实现了批量移除文件名中的指定字符串,无法进行替换,本文在前面工作的基础上,增加批量替换文件名中指定字符串的功能。
新增的功能点不多,主要包括:
批量修改文件名程序的完整代码如所示:
# coding=gbk import tkinter as tk import os from tkinter.filedialog import askdirectory def BrowseDri(): txtDirPath.set(askdirectory()) def SetControlStatus(): mode=processMode.get() if(mode==1): txtRemoved['state'] = 'normal' txtBeforeReplaced['state'] = 'disabled' txtAfterReplaced['state'] = 'disabled' btnProcess['text']='移除' elif mode==2: txtRemoved['state'] = 'disabled' txtBeforeReplaced['state'] = "normal" txtAfterReplaced['state'] = 'normal' btnProcess['text']='替换' def BatchReplaceFileName(): path = txtDirPath.get() mode=processMode.get() if(mode==1): strOldSign=txtRemovedContent.get() strNewSign="" elif mode==2: strOldSign=txtBeforeReplactContent.get() strNewSign=txtAfterReplactContent.get() files=os.listdir(path) for onefile in files: if onefile.find(strOldSign)<0: continue oldname=path+"\\"+onefile newname=path+"\\"+onefile.replace(strOldSign,strNewSign) os.rename(oldname,newname) print(oldname,"====>",newname) window=tk.Tk() window.title('批量处理文件名') window.geometry('400x300') tk.Label(window,text='选择文件夹').grid(row=0,column=0) txtDirPath=tk.StringVar() tk.Entry(window,textvariable=txtDirPath).grid(row=0,column=1) tk.Button(window,text='浏览',command=BrowseDri).grid(row=0,column=2) processMode =tk.IntVar() tk.Radiobutton(window, text="移除内容", variable=processMode, value=1, command=SetControlStatus).grid(row=1,column=0) tk.Label(window,text='输入要移除的内容:').grid(row=1,column=1) txtRemovedContent=tk.StringVar() txtRemoved=tk.Entry(window,textvariable=txtRemovedContent) txtRemoved.grid(row=1,column=2) tk.Radiobutton(window, text="替换内容", variable=processMode, value=2, command=SetControlStatus).grid(row=2,column=0) tk.Label(window,text='输入替换前的内容:').grid(row=2,column=1) txtBeforeReplactContent=tk.StringVar() txtBeforeReplaced=tk.Entry(window,textvariable=txtBeforeReplactContent,state='disabled') txtBeforeReplaced.grid(row=2,column=2) tk.Label(window,text='输入替换后的内容:').grid(row=3,column=1) txtAfterReplactContent=tk.StringVar() txtAfterReplaced=tk.Entry(window,textvariable=txtAfterReplactContent,state='disabled') txtAfterReplaced.grid(row=3,column=2) processMode.set(1) btnProcess=tk.Button(window,text='移除',command=BatchReplaceFileName) btnProcess.grid(row=4,column=0) tk.mainloop()
最后是程序效果,如下图所示,选择指定文件夹,首先将文件夹中所有文件中的car字符串替换为che@,接着再移除文件名中的@字符。