我当前正在编写一个Tkinter程序,该程序重命名所选目录中相同类型的所有文件。但是,每当我使用bulkRename()
函数时,都会出现上述错误。
该函数应该采用给定的文件名作为基础(例如:my_image
),然后接收原始扩展名(例如:.jfif
),然后采用新的扩展名(例如。 .jpg
),然后用新的替换旧的。然后,该函数通过在名称末尾添加一个数字(例如:my_image1.jpg
,my_image2.jpg
等)进行迭代,并继续对相同类型的所有文件执行此操作。我相对较新,因此不胜感激。
到目前为止,我的代码如下:
from tkinter import *
from tkinter import filedialog
import os
import sys
import glob
window = Tk()
window.title("Bulk File Renamer")
window.minsize(400,300)
# Filename
def storeVal1():
global val1
val1 = entry1.get()
# Old Extension
def storeVal2():
global val2
val2 = entry2.get()
# New Extension
def storeVal3():
global val3
val3 = entry3.get()
def dirChoice():
global path
path = filedialog.askdirectory()
def bulkRename():
i = 0
for fname in os.listdir(path):
if fname.endswith(val2):
fname = val1 + str(i)
fname.replace(val2,val3)
os.rename(fname,path)
i += 1
label1 = Label(window, text="Desired Filename")
label1.pack()
entry1 = Entry(window)
entry1.pack()
button1 = Button(window, text="Confirm",command=storeVal1)
button1.pack()
label2 = Label(window, text="Old Extension")
label2.pack()
entry2 = Entry(window)
entry2.pack()
button2 = Button(window, text="Confirm",command=storeVal2)
button2.pack()
label3 = Label(window, text="New Extension")
label3.pack()
entry3 = Entry(window)
entry3.pack()
button3 = Button(window, text="Confirm",command=storeVal3)
button3.pack()
button4 = Button(window, text="Choose Directory", command=dirChoice)
button4.pack()
button5 = Button(window, text="Rename files",command=bulkRename)
button5.pack()
window.mainloop()
FileNotFoundError:[WinError 2]系统找不到指定的文件:'new_text0'->'C:/ Users /......'