【python】重命名文件工具(练习)

from tkinter import *
import tkinter
import tkinter.filedialog
import  os
import tkinter.messagebox

#获取扩展名
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 hint():
    result=tkinter.messagebox.askokcancel('提示', '要执行此操作吗')
    return result
def  renamefile():
    result=hint()
    if result==False:
        return
    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()# 进入消息循环

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值