from tkinter import *
import tkinter
import tkinter.filedialog
import os
#获取扩展名
def GetFileExtension(path):
return os.path.splitext(path)[1]
def choose_file():
selectFileName = tkinter.filedialog.askdirectory(title='选择文件夹') # 选择文件
pathtext.set(selectFileName)
displaylistbox(pathtext.get(),sourcelistbox,sourcesuffix.get())
def displaylistbox(pathdir, listboxname,suffix):
Flist = listfilename(pathdir,suffix)
listboxname.delete(0, END)
for item in Flist: # 第一个小部件插入数据
listboxname.insert(0, item)
def renamefile():
selectFileName=pathtext.get()
if selectFileName == "":
return
Flist=listfilename(selectFileName,sourcesuffix.get())
for filename in Flist:
portion = os.path.splitext(filename) # 分离文件名字和后缀
newname = portion[0]+descsuffix.get()#要改的新后缀
descfullfilename = os.path.join(selectFileName, newname)
sorucefullfilename = os.path.join(selectFileName, filename)
os.rename(sorucefullfilename,descfullfilename)
displaylistbox(pathtext.get(), desclistbox, descsuffix.get())
def listfilename(path,suffix):
files = os.listdir(path)#列出当前目录下所有的文件
filenamelist=[]
fulldirct = ""
for v in files:
if len(suffix) == 0 or suffix == '*' or suffix=='.*':
fulldirct = os.path.join(path, v)
else:
if GetFileExtension(v).lower() == suffix:
fulldirct = os.path.join(path, v)
if os.path.isfile(fulldirct): #需要绝对路径
filenamelist.append(v)
return filenamelist
def refreshfile():
displaylistbox(pathtext.get(), sourcelistbox, sourcesuffix.get())
displaylistbox(pathtext.get(), desclistbox, descsuffix.get())
window = tkinter.Tk()
window.title = '重名名工具'
window.geometry('860x680')
scrolly = Scrollbar(window)
scrolly.pack(side=RIGHT, fill=Y)
#路径文本框
pathtext = tkinter.StringVar()
pathtext.set("./")
e_entry = tkinter.Entry(window, width=48, textvariable=pathtext).place(x=10, y=10)
#设定两个文件列表框
xwidth=40
yheight=20
xplace=50
yplace=200
sourcelistbox=Listbox(window,width=xwidth,height=yheight,bg="#fffccf", yscrollcommand=scrolly.set)
sourcelistbox.place(x=xplace,y=yplace)
desclistbox = Listbox(window,width=xwidth,height=yheight,bg="#fffccf",yscrollcommand=scrolly.set)
desclistbox.place(x=xplace+400,y=yplace)
tkinter.Label(window, text='重命名前文件列表', font=('Arial', 14)).place(x=xplace, y=yplace-30)
tkinter.Label(window, text='重命名后文件列表', font=('Arial', 14)).place(x=xplace+400, y=yplace-30)
tkinter.Label(window, text='=====>', font=('Arial', 14)).place(x=350, y=400)
scrolly.config(command=sourcelistbox.yview)
#配置listbox
xplace=10
yplace=50
tkinter.Label(window, text='指定重命名的文件扩展名:', font=('Arial', 14)).place(x=xplace, y=yplace)
sourcesuffix = tkinter.StringVar()
sourcesuffix.set('.*')
filesuffix = tkinter.Entry(window, textvariable=sourcesuffix, font=('Arial', 14))
filesuffix.place(x=240 + xplace, y=yplace+5)
tkinter.Label(window, text='重命名后的文件扩展名:', font=('Arial', 14)).place(x=xplace, y=yplace+40)
descsuffix = tkinter.StringVar()
descsuffix.set('.sql')
filesuffix = tkinter.Entry(window, textvariable=descsuffix, font=('Arial', 14))
filesuffix.place(x=240 + xplace, y=yplace+40)
#初始化显示默认路径的文件列表
displaylistbox(pathtext.get(), sourcelistbox,sourcesuffix.get())
submit_button = tkinter.Button(window, text ="选择文件夹", command = choose_file)
submit_button.pack()
submit_button = tkinter.Button(window, text ="重命名", command = renamefile)
submit_button.place(x=350,y=320)
submit_button = tkinter.Button(window, text ="刷新", command = refreshfile)
submit_button.place(x=350,y=360)
window.mainloop()# 进入消息循环