from tkinter import *
import tkinter
import tkinter.filedialog
import os
import tkinter.messagebox
def GetFileExtension(path):
return os.path.splitext(path)[1]
def choosesourcepath():
selectFileName = tkinter.filedialog.askdirectory(title='选择文件夹')
sourcepathtext.set(selectFileName)
def choosedescpath():
selectFileName = tkinter.filedialog.askdirectory(title='选择文件夹')
descpathtext.set(selectFileName)
def hint(filename,newname):
result=tkinter.messagebox.askokcancel('提示', '要执行此操作吗 ?\n '+"示例:" + filename + " ==> " +newname)
return result
def hint_finish():
tkinter.messagebox.askokcancel('提示', '转换完成!')
def modifyname():
serial=beginserial.get()
files = os.listdir(sourcepathtext.get())
files.sort()
continueflag = False
for filename in files:
if filename == runname:
continue
srcfullname = sourcepathtext.get() + "/" + filename
allflagnum=srcfullname.count(splitflag.get())
orifilename= filename.split(str(splitflag.get()))
splitnum=splitpostion.get()
if allflagnum < splitnum:
splitnum = allflagnum
orifilename=str(splitflag.get()).join(orifilename[splitnum::])
print(orifilename)
if os.path.isfile(srcfullname):
newname="T" + str(serial).zfill(3)+"_"+ orifilename
if continueflag == False:
result = hint(filename,newname)
if result == False:
return
else:
continueflag=True
os.rename(sourcepathtext.get()+"/"+filename,sourcepathtext.get()+"/"+newname)
serial=int(serial)+1
hint_finish()
runname = os.path.basename(sys.argv[0])
window = tkinter.Tk()
window.title('批量序号命名工具 作者:xiaym 日期:2019-09-10')
window.geometry('600x250')
xplace=10
yplace=10
sourcepathtext = tkinter.StringVar()
sourcepathtext.set("./")
e_entry = tkinter.Entry(window, width=48, textvariable=sourcepathtext).place(x=xplace, y=yplace)
beginserial = tkinter.StringVar()
beginserial.set("10")
e_entry = tkinter.Entry(window, width=48, textvariable=beginserial).place(x=xplace, y=yplace*5)
splitflag = tkinter.StringVar()
splitflag.set("_")
e_entry = tkinter.Entry(window, width=20, textvariable=splitflag).place(x=xplace, y=yplace*10)
tkinter.Label(window, text='表名分割符').place(x=xplace*20, y=yplace*10)
splitpostion = tkinter.IntVar()
splitpostion.set("1")
e_entry = tkinter.Entry(window, width=20, textvariable=splitpostion).place(x=xplace , y=yplace*15)
tkinter.Label(window, text='第几个分割符后做为文件名').place(x=xplace*20, y=yplace*15)
files = os.listdir(sourcepathtext.get())
submit_button = tkinter.Button(window, text ="选择文件夹", command = choosesourcepath).place(x=xplace*40, y=yplace)
tkinter.Label(window, text='文件的序号').place(x=xplace*40, y=yplace*5)
submit_button = tkinter.Button(window, text =" 转换 ", command = modifyname).place(x=xplace*20, y=yplace*20)
window.mainloop()