1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import os
- from posixpath import dirname
- import xlrd
- import tkinter as tk
- from tkinter import filedialog as tkFileDialog
- from tkinter import messagebox
- import shutil
-
- def start():
- window = tk.Tk() # 创建窗口对象的背景色
- window.title('批量文件改名程序') # 设置窗口的标题
- window.geometry('400x300') # 设置窗口的大小
- title = tk.Label(window, text='批量文件改名程序',
- font=('微软雅黑', 12), width=40, height=3)
- title.place(x=0, y=0, anchor='nw')
- choosepath = tk.Label(window, text='选择文件夹:',
- font=('微软雅黑', 12), width=20, height=2)
- choosepath.place(x=0, y=50, anchor='nw')
- path_var1 = tk.StringVar() # 输入路径
- entry1 = tk.Entry(window, textvariable=path_var1)
- entry1.place(x=170, y=65, anchor='nw')
- ori = tk.Label(window, text='替换前内容:',
- font=('微软雅黑', 12), width=20, height=2)
- ori.place(x=0, y=100, anchor='nw')
- path_var2 = tk.StringVar() # 输出路径
- entry2 = tk.Entry(window, textvariable=path_var2)
- entry2.place(x=170, y=115, anchor='nw')
- new = tk.Label(window, text='替换后内容:',
- font=('微软雅黑', 12), width=20, height=2)
- new.place(x=0, y=150, anchor='nw')
- path_var3 = tk.StringVar() # 输出路径
- entry3 = tk.Entry(window, textvariable=path_var3)
- entry3.place(x=170, y=165, anchor='nw')
-
- def choose1():
- file_dir1 = tkFileDialog.askdirectory()
- path_var1.set(file_dir1)
-
- def tuichu():
- window.destroy()
-
- def main():
- inpath = entry1.get()
- ori = entry2.get()
- new = entry3.get()
- #遍历文件夹
- for parent,dirname,filenames in os.walk(inpath):
- for filename in filenames:
- #排一下其他文件
- if ori in filename:
- filepath = parent + '/' +filename
- newname = filename.replace(ori,new)
- nfilepath = parent + '/' +newname
- os.rename(filepath,nfilepath)
- messagebox.showinfo("消息","运行成功")
- window.destroy()
-
- tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
- tk.Button(window,text='确认',command=main).place(x=120,y=230,anchor='nw')
- tk.Button(window,text='取消',command=tuichu).place(x=220,y=230,anchor='nw')
-
- window.mainloop()
-
- if __name__ == '__main__':
- start()
|