import os import sys from tkinter import Tk from tkinter import Label from tkinter import Button from tkinter import StringVar def showInfo(): info.clear() for d in os.listdir(path): fullDirPath = os.path.join(path, d) if d[:2] != 'XN' and os.path.isdir(fullDirPath): names = [] for filename in os.listdir(fullDirPath): if filename[:2] == 'XN' and os.path.isfile(os.path.join(fullDirPath, filename)): # XNK1-G1-190101-AA- l = filename.replace(' ', '').split('-') for index in range(len(l)): if len(l[index]) == 6 and l[index].isdigit(): names.append(['-'.join(l[:index+2]), os.path.splitext(filename)[0]]) break if len(names) == 1: info.append([d, names[0][0], names[0][1]]) text = '' for i in info: text = text + i[0] + ' -> ' + i[1] + '\n' var.set(text) def rename(): if len(info) > 0: with open('renameInfo.txt', 'a') as file: for i in info: os.rename(os.path.join(path, i[0]), os.path.join(path, i[1])) file.write(i[2]+'\n') showInfo() window = Tk() window.title('Rename Tool') window.geometry('250x300') var = StringVar() # should after Tk() function info = [] path = '' Label(window, textvariable=var, justify='left').pack(fill='both', expand='yes') if len(sys.argv) == 2: path = sys.argv[1] showInfo() Button(window, text='Refresh', command=showInfo).pack(fill='x', padx=10, pady=5) Button(window, text='Rename', command=rename).pack(fill='x', padx=10, pady=5) else: var.set('Use like: RenameTool.exe YourPath') window.mainloop()
https://files.cnblogs.com/files/howld/RenameTool.zip