【python】重命名文件工具(练习)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【python】重命名文件工具(练习),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3491字,纯文字阅读大概需要5分钟。
内容图文
![【python】重命名文件工具(练习)](/upload/InfoBanner/zyjiaocheng/719/b5b5e23168084668a798fa582311b4cc.jpg)
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()# 进入消息循环
内容总结
以上是互联网集市为您收集整理的【python】重命名文件工具(练习)全部内容,希望文章能够帮你解决【python】重命名文件工具(练习)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。