转自
【python小工具】批量修改所选文件夹中所有文件的名称
源码
注意事项,相同文件名,修改按键只能点击一次,因为加载的列表是无序。第二次点击会造成素材减少。
import tkinter as tk
from tkinter import filedialog # 文件夹路径
from tkinter import messagebox # 弹出窗口
import os # 系统
# 方法
# 选择文件夹
def openFolderPath():
selectFolderPath = filedialog.askdirectory()
if(selectFolderPath != ''):
folderPath.set(selectFolderPath)
findAllFilesInTheFolder()
# 读取所选文件夹中所有文件
def findAllFilesInTheFolder():
t_filesList = os.listdir(folderPath.get())
filesList.set(t_filesList)
t_folderNumber = 0
t_fileNumber = 0
for fileItem in t_filesList:
file = os.path.splitext(fileItem)
if(file[1] == ''):
t_folderNumber += 1
else:
t_fileNumber += 1
labelFilesInfor.config(text='当前目录下有' + str(t_fileNumber) + '个文件,' + str(t_folderNumber) + '个文件夹')
# 确认修改所选文件夹中的所有文件名
def startToRenameSelectedFiles():
if(setFileNameHead.get() != '' and folderPath.get() != ''):
tempIndex = 1
t_path = folderPath.get()
t_filesList = os.listdir(t_path)
for fileItem in t_filesList:
file = os.path.splitext(fileItem)
if(file[1] != ''):
print('修改前:' + fileItem)
file_foot = file[1] # 保存后缀名
os.rename(t_path + '/' + fileItem,t_path + '/'+ setFileNameHead.get() + str(tempIndex) + file_foot)
print('修改后:' + setFileNameHead.get() + str(tempIndex) + file_foot)
tempIndex += 1
messagebox.showinfo(title=theToolName,message='修改文件名成功')
elif(setFileNameHead.get() == '' and folderPath.get() != ''):
messagebox.showerror(title=theToolName,message='请输入前缀名')
else:
messagebox.showerror(title=theToolName,message='请选择正确的文件夹路径')
# 打开所修改的文件夹路径
def openTheFolder():
os.system("start explorer "+(folderPath.get()).replace('/','\\'))
# 实例化窗口
window = tk.Tk()
# 窗口起名
theToolName = '天羊-批量修改文件名v0.1'
window.title(theToolName)
# 设置窗口大小
window.geometry('600x300')
window.resizable(width=False, height=False) # 禁止拉伸窗口
# 设置变量
folderPath = tk.StringVar() # 文件夹路径
filesList = tk.StringVar() # 文件夹路径下所有的文件
setFileNameHead = tk.StringVar() # 修改格式中文件名前缀
setFileNameFoot = tk.StringVar() # 修改格式中文件名后缀
# 标签设置
labelPath = tk.Label(window,text='请选择想要修改文件名的文件所处路径',font=('Arial',12),width=0,height=1)
labelPath.place(x=20,y=10)
labelFiles = tk.Label(window,text='当前路径下的文件列表',font=('Arial',12),width=0,height=1)
labelFiles.place(x=20,y=80)
labelSetting = tk.Label(window,text='请设置修改的格式',font=('Arial',12),width=0,height=1)
labelSetting.place(x=400,y=10)
labelSetting = tk.Label(window,text='文件名前缀',font=('Arial',10),width=0,height=1)
labelSetting.place(x=400,y=40)
labelSetting = tk.Label(window,text='文件名后缀',font=('Arial',10),width=0,height=1)
labelSetting.place(x=400,y=80)
labelFilesInfor = tk.Label(window,text='当前目录下有0个文件,0个文件夹',font=('Arial',12),width=0,height=1)
labelFilesInfor.place(x=20,y=250)
# 文本输入
insertPath = tk.Entry(window,textvariable=folderPath,width=30,state='readonly')
insertPath.place(x=20,y=40) # 所选择的文件夹路径
insertSetHead = tk.Entry(window,textvariable=setFileNameHead,width=20)
insertSetHead.place(x=400,y=60) # 修改格式中文件名前缀
# 选项设置
setFileFoot2 = tk.Radiobutton(window,text='1(1,2,3...)',variable=setFileNameFoot,value=1)
setFileFoot2.place(x=400,y=100)
# 按钮设置
buttonInsertPath = tk.Button(window,text='选择路径',font=('Arial',12),width=10,height=1,command=openFolderPath)
buttonInsertPath.place(x=250,y=40) # 选择文件夹按钮
buttonStartRename = tk.Button(window,text='确认修改',font=('Arial',12),width=10,height=1,command=startToRenameSelectedFiles)
buttonStartRename.place(x=450,y=250) # 确认修改按钮
# 文件列表设置
filesListBox = tk.Listbox(window,listvariable=filesList,width=40,height=7)
filesListBox.place(x=20,y=100)
# 测试按钮
insertPathButton = tk.Button(window,text='打开文件夹',font=('Arial',12),width=10,height=1,command=openTheFolder)
insertPathButton.place(x=330,y=250)
# 主窗口循环显示
window.mainloop()
仿写应用1,视频拆分成图片
import tkinter as tk
from tkinter import filedialog # 文件夹路径
from tkinter import messagebox # 弹出窗口
from tkinter import ttk # 导入ttk模块,因为下拉菜单控件在ttk中
import os # 系统
import cv2
filedpath = ['videopath','outputpath']
# 方法
# 选择文件夹
def openFolderPath():
selectFolderPath = filedialog.askdirectory()
filedpath[0] = selectFolderPath
if(selectFolderPath != ''):
folderPath.set(selectFolderPath)
findAllFilesInTheFolder()
# 选择输出文件夹
def openOutputFolderPath():
selectFolderPath = filedialog.askdirectory()
filedpath[1] = selectFolderPath
if(selectFolderPath != ''):
outputfolderPath.set(selectFolderPath)
# 读取所选文件夹中所有文件
def findAllFilesInTheFolder():
t_filesList = os.listdir(folderPath.get())
filesList.set(t_filesList)
t_folderNumber = 0
t_fileNumber = 0
for fileItem in t_filesList:
file = os.path.splitext(fileItem)
if(file[1] == ''):
t_folderNumber += 1
else:
t_fileNumber += 1
labelFilesInfor.config(text='当前目录下有' + str(t_fileNumber) + '个文件,' + str(t_folderNumber) + '个文件夹')
# 将视频拆分为图片
def video2image():
if (folderPath.get() ==' ' ):
messagebox.showinfo(title=theToolName, message='请选择正确的视频文件夹路径')
elif (outputfolderPath.get() == ''):
messagebox.showerror(title=theToolName, message='请选择正确的输出文件夹路径')
else:
videoPath =filedpath[0]
outPath = filedpath[1]
filesList= os.listdir(videoPath)
for fileItem in filesList:
file = fileItem.split('.')
if (file[-1] == 'mp4' or file[-1] == 'avi'):
videosrc = videoPath + '/' + fileItem
print(videosrc)
# 创建文件夹
t_file = os.path.splitext(fileItem)
fileName = outPath + '/' + t_file[0]
if not os.path.exists(fileName):
os.makedirs(fileName)
vc = cv2.VideoCapture(videosrc) # 设置视频位置
c = 1
if vc.isOpened():
rval, frame = vc.read()
else:
rval = False
# 设置帧数
farmeRate = setFrameRate.get()
timeF = int("".join(list(filter(str.isdigit, farmeRate))))
while rval:
rval, frame = vc.read()
if (c % timeF == 0):
cv2.imwrite(fileName + '/' + t_file[0] + str(c) + '.jpg', frame) # 设置图片存储的位置
c = c + 1
cv2.waitKey(1)
vc.release()
messagebox.showinfo(title=theToolName, message='修改文件名成功')
# 实例化窗口
window = tk.Tk()
# 窗口起名
theToolName = '批量视频抽帧 v0.1'
window.title(theToolName)
# 设置窗口大小
window.geometry('600x350')
window.resizable(width=False, height=False) # 禁止拉伸窗口
# 设置变量
folderPath = tk.StringVar() # 文件夹路径
filesList = tk.StringVar() # 文件夹路径下所有的文件
setFileNameHead = tk.StringVar() # 修改格式中文件名前缀
setFileNameFoot = tk.StringVar() # 修改格式中文件名后缀
setFrameRate = tk.StringVar() # 修改文抽帧频率
outputfolderPath = tk.StringVar() # 输出文件夹路径
# 标签设置
labelPath = tk.Label(window,text='请选择想要修改视频文件所处路径',font=('Arial',12),width=0,height=1)
labelPath.place(x=20, y=10)
labelPath = tk.Label(window,text='请选择图片输出的文件所处路径',font=('Arial',12),width=0,height=1)
labelPath.place(x=20, y=80)
labelFiles = tk.Label(window,text='当前路径下的文件列表',font=('Arial',12),width=0,height=1)
labelFiles.place(x=20, y=150)
labelSetting = tk.Label(window,text='请设置修改的格式',font=('Arial',12),width=0,height=1)
labelSetting.place(x=400, y=10)
labelSetting = tk.Label(window,text='抽帧频率',font=('Arial',10),width=0,height=1)
labelSetting.place(x=400, y=40)
labelFilesInfor = tk.Label(window,text='当前目录下有0个文件,0个文件夹',font=('Arial',12),width=0,height=1)
labelFilesInfor.place(x=20, y=300)
# 文本输入
insertPath = tk.Entry(window,textvariable=folderPath,width=30,state='readonly')
insertPath.place(x=20,y=40) # 所选择的文件夹路径
insertPath2 = tk.Entry(window,textvariable=outputfolderPath,width=30,state='readonly')
insertPath2.place(x=20,y=110) # 所选择的文件夹路径
# 创建下拉菜单
cmb = ttk.Combobox(window, textvariable=setFrameRate)
cmb.place(x=400, y=80)
cmb['value'] = ('1', '5', '10', '20')
cmb.current(2)
cmb.bind(video2image)
# 按钮设置
buttonInsertPath = tk.Button(window,text='选择视频路径',font=('Arial',12),width=10,height=1,command=openFolderPath)
buttonInsertPath.place(x=250,y=40) # 选择文件夹按钮
buttonInsertPath = tk.Button(window,text='选择输出路径',font=('Arial',12),width=10,height=1,command=openOutputFolderPath)
buttonInsertPath.place(x=250,y=110) # 选择文件夹按钮
buttonStartRename = tk.Button(window,text='确认修改',font=('Arial',12),width=10,height=1,command=video2image)
buttonStartRename.place(x=450,y=300) # 确认修改按钮
# 文件列表设置
filesListBox = tk.Listbox(window,listvariable=filesList,width=40,height=5)
filesListBox.place(x=20,y=170)
filesListBox = tk.Listbox(window,listvariable=setFrameRate,width=5,height=1)
filesListBox.place(x=480,y=40)
# 主窗口循环显示
window.mainloop()