Python 目錄下文件重命名
#Time: 2020/04/27
#Author: Xiaohong
#運行環境: OS: Windows 7
# Python: 3.7
背景: 有時從網絡下載的mp3文件,有按數字編號,但沒有考慮到字符排序,會造成收聽時不連貫,故用如下過程重命名,方便收聽..如下是完整程式:
效果圖爲:
主程式爲:
# -*- coding:utf-8 -*-
import tkinter as tk
from tkinter.ttk import *
from tkinter import ttk
from Tl_class_frame import *
import os
import time
from tkinter.filedialog import askopenfilename
from shutil import copy
import re
class PiMain(object):
#設計UI 界面
def setupUI(self):
v = View(self.root, kind='日', orient=tk.VERTICAL)
v.v1['width'] = 120
# #增加按鈕,用來 OS
vButton = tk.Button(v.v1,text='待擴展1',width=11)
vButton.place(anchor=tk.W,relx=0.1,rely=0.2)
vButton = tk.Button(v.v1, text='待擴展2', width=11)
vButton.place(anchor=tk.W, relx=0.1, rely=0.3)
vButton = tk.Button(v.v1, text='待擴展3', width=11)
vButton.place(anchor=tk.W, relx=0.1, rely=0.4)
v_right = View(v.v2, bg='blue',kind='日', orient=tk.HORIZONTAL)
v_right.v1['height'] = 100
# #增加Label,用來
tk.Label(v_right.v1,text='文件夾路徑:').pack(side=tk.LEFT)
self.file1=tk.StringVar()
tk.Entry(v_right.v1,width=50,textvariable=self.file1).pack(side=tk.LEFT)
vButton = tk.Button(v_right.v1, text='...', width=3,command=self.getfile)
vButton.pack(side=tk.LEFT)
vButton = tk.Button(v_right.v1, text='重命名', width=8,command=self.renamefile)
vButton.pack(side=tk.LEFT)
v_right2 = View(v_right.v2, bg='blue',kind='日', orient=tk.VERTICAL)
# v_right2.v1['height'] = 100
lf1=tk.LabelFrame(v_right2.v1,text="原來文件列表")
lf1.pack()
self.lb1=tk.Listbox(lf1,height=30,width=50)
self.lb1.insert('end','xxxxxxx')
self.lb1.pack()
lf2=tk.LabelFrame(v_right2.v2,text="重命名後的文件列表")
lf2.pack()
self.lb2=tk.Listbox(lf2,height=30,width=50)
self.lb2.insert('end','yyyyyy')
self.lb2.pack()
def renamefile(self):
direct1=self.file1.get() #獲得 要處理的文件夾 路徑
direct2=self.get_sysdate2() #獲得 備份文件夾 名字
parent_path = os.path.dirname(direct1) #獲得 要處理的文件夾 所在的目錄,即 父級目錄
bak_direct1=os.path.join(parent_path,direct2) #得到 備份文件夾 的完整路徑
file_lists=[]
file_list2s=[]
if not os.path.exists(bak_direct1): #判斷 備份文件夾 是否已存在
os.makedirs(bak_direct1) # 不存在則重新創建
for root,sub_dirs,files in os.walk(direct1): # 遍歷 要處理的文件夾
for file in files:
if file.endswith('mp3'): #檢查 擴展名爲MP3的文件
print(file)
file_lists.append(file)
dest_file=os.path.join(bak_direct1,file) #要備份的 目標文件名
copy(os.path.join(root,file),dest_file) #Copy 到目標處
vir_file=os.path.splitext(file)[0] #除去 擴展名
vir_file_ext=os.path.splitext(file)[1] # 擴展名
num_file=re.findall(r"\d+",vir_file) # 用正則提取 文件名中的數字
if num_file: #非空,則判斷是否 小於10,是則在前面加0
if int(num_file[0])<10 and len(num_file[0])<2 :
pre_name='0'+(num_file[0])
else:
pre_name=num_file[0]
new_file_name=pre_name+file #在原有文件名前加上 前綴
print(new_file_name)
file_list2s.append(new_file_name)
os.rename(os.path.join(root, file), os.path.join(root, new_file_name))
for file in file_lists:
self.lb1.insert('end',file)
for file in file_list2s:
self.lb2.insert('end',file)
def handler(self, event, top, tv):
content = self.tv.set(self.tv.selection(), column='ColB')
# print(content)
if content=="":
pass
else:
self.show_vip11_data(content)
def handler_adaptor(self, fun, **kwds):
# 事件處理函數的適配器,相當於中介,那個event是從那裏來的呢,我也納悶,這也許就是python的偉大之處吧
return lambda event, fun=fun, kwds=kwds: fun(event, **kwds)
def getfile(self):
xx = tk.filedialog.askdirectory()
self.file1.set(xx)
# 設立函數,來取得當前時間,作爲文件名的一部分,以免文件名重複
def get_sysdate2(self):
now = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
# print(now)
return now
def setCenter(self,root, w, h):
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = int((ws / 2) - (w / 2))
y = int((hs / 2) - (h / 2))
root.geometry('{}x{}+{}+{}'.format(w, h, x, y))
#類的運行主體
def __init__(self):
self.root = tk.Tk()
self.root.title(string='MP3 文件重命名 管理')
self.setCenter(self.root, 700, 300)
self.setupUI()
self.root.mainloop()
if __name__=='__main__':
PiMain()
其中,調用了類:Tl_class_frame 如下:
# -*- coding:utf-8 -*-
import tkinter as tk
class View(tk.Frame):
def __init__(self,master=None,kind='口',orient=tk.HORIZONTAL,**kw):
tk.Frame.__init__(self,master,**kw)
self.root=master
self.kind=kind
self.orient=orient
self.m=1
self.v=[]
self.left=None
self.right=None
self.top=None
self.top=None
self.pack(fill=tk.BOTH,expand=1)
if self.kind=='口':
self.view1()
elif self.kind=='日':
if self.orient==tk.HORIZONTAL:
self.view2a()
else:
self.view2b()
elif self.kind=='目':
if self.orient == tk.HORIZONTAL:
self.view3a()
else:
self.view3b()
def view1(self):
self.m=1
self.v1=self
self.v1.pack(side=tk.TOP, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v.append(self.v1)
def view2a(self):
self.m=2
self.v1=tk.Frame(self)
self.v2=tk.Frame(self)
self.top=self.v1
self.bottom=self.v2
# self.v1.pack(side=tk.TOP,fill=tk.BOTH,expand=1,ipady=1,pady=1,ipadx=1,padx=1)
self.v1.pack(side=tk.TOP, fill=tk.X, ipady=1, pady=1, ipadx=1, padx=1)
self.v2.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v.append(self.v1)
self.v.append(self.v2)
def view2b(self):
self.m=2
# self.v1=tk.Frame(self,width=200)
self.v1 = tk.Frame(self)
self.v2=tk.Frame(self)
self.left=self.v1
self.right=self.v2
# self.v1.pack(side=tk.LEFT,fill=tk.BOTH,expand=1,ipady=1,pady=1,ipadx=1,padx=1)
self.v1.pack(side=tk.LEFT,fill=tk.Y, ipady=1, pady=1, ipadx=1, padx=1)
self.v2.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v.append(self.v1)
self.v.append(self.v2)
def view3a(self):
self.m=3
self.v1 = tk.Frame(self)
self.v2 = tk.Frame(self)
self.v3 = tk.Frame(self)
self.top = self.v1
self.center = self.v2
self.bottom = self.v3
self.v1.pack(side=tk.TOP, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v2.pack(side=tk.TOP, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v3.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v.append(self.v1)
self.v.append(self.v2)
self.v.append(self.v3)
def view3b(self):
self.m=3
self.v1 = tk.Frame(self)
self.v2 = tk.Frame(self)
self.v3 = tk.Frame(self)
self.left = self.v1
self.center = self.v2
self.right = self.v3
self.v1.pack(side=tk.LEFT, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v2.pack(side=tk.LEFT, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v3.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1, ipady=1, pady=1, ipadx=1, padx=1)
self.v.append(self.v1)
self.v.append(self.v2)
self.v.append(self.v3)