工具箱相关
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RenameFile.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. from posixpath import dirname
  3. import xlrd
  4. import tkinter as tk
  5. from tkinter import filedialog as tkFileDialog
  6. from tkinter import messagebox
  7. import shutil
  8. def start():
  9. window = tk.Tk() # 创建窗口对象的背景色
  10. window.title('批量文件改名程序') # 设置窗口的标题
  11. window.geometry('400x300') # 设置窗口的大小
  12. title = tk.Label(window, text='批量文件改名程序',
  13. font=('微软雅黑', 12), width=40, height=3)
  14. title.place(x=0, y=0, anchor='nw')
  15. choosepath = tk.Label(window, text='选择文件夹:',
  16. font=('微软雅黑', 12), width=20, height=2)
  17. choosepath.place(x=0, y=50, anchor='nw')
  18. path_var1 = tk.StringVar() # 输入路径
  19. entry1 = tk.Entry(window, textvariable=path_var1)
  20. entry1.place(x=170, y=65, anchor='nw')
  21. ori = tk.Label(window, text='替换前内容:',
  22. font=('微软雅黑', 12), width=20, height=2)
  23. ori.place(x=0, y=100, anchor='nw')
  24. path_var2 = tk.StringVar() # 输出路径
  25. entry2 = tk.Entry(window, textvariable=path_var2)
  26. entry2.place(x=170, y=115, anchor='nw')
  27. new = tk.Label(window, text='替换后内容:',
  28. font=('微软雅黑', 12), width=20, height=2)
  29. new.place(x=0, y=150, anchor='nw')
  30. path_var3 = tk.StringVar() # 输出路径
  31. entry3 = tk.Entry(window, textvariable=path_var3)
  32. entry3.place(x=170, y=165, anchor='nw')
  33. def choose1():
  34. file_dir1 = tkFileDialog.askdirectory()
  35. path_var1.set(file_dir1)
  36. def tuichu():
  37. window.destroy()
  38. def main():
  39. inpath = entry1.get()
  40. ori = entry2.get()
  41. new = entry3.get()
  42. #遍历文件夹
  43. for parent,dirname,filenames in os.walk(inpath):
  44. for filename in filenames:
  45. #排一下其他文件
  46. if ori in filename:
  47. filepath = parent + '/' +filename
  48. newname = filename.replace(ori,new)
  49. nfilepath = parent + '/' +newname
  50. os.rename(filepath,nfilepath)
  51. messagebox.showinfo("消息","运行成功")
  52. window.destroy()
  53. tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
  54. tk.Button(window,text='确认',command=main).place(x=120,y=230,anchor='nw')
  55. tk.Button(window,text='取消',command=tuichu).place(x=220,y=230,anchor='nw')
  56. window.mainloop()
  57. if __name__ == '__main__':
  58. start()