Python實現文件整理(依照文件創建時間)

前言

前幾天需要做一個視頻,是一個活動的全程錄像,大量的mpg格式的視頻,需要從中挑選精品視頻按活動流程,用pr做成一個片頭,需要有整個活動大體流程,不超過5分鐘。面對大量視頻,能不能用python讓視頻按時間歸類,然後我每個時間段都可以選出一些視頻,保證時間順序。

思路

對於當下熱門語言Python,自然不缺少IO操作庫,通過一陣檢索,找到shutil庫

shuil模塊提供了許多對文件和文件集合的高級操作。特別是,提供了支持文件複製和刪除的函數。

經過簡單學習,已有初步瞭解。
大體流程;

  1. 遍歷目標文件夾
  2. 獲取當前文件的創建時間
  3. 判斷當前文件夾 是否含有 以 當前文件的創建時間 爲名的 文件夾
  4. 如果有,移動該文件到目標時間文件夾;否則,創建目標時間文件夾,移動。

動手

對於文件整理小工具,想必以後也可以用到,遂用tk庫來將此做成桌面應用,程序忽略文件夾,僅對文件進行處理,不判斷文件什麼格式。
文件分創建時間,修改時間可以通過文件屬性查看,當然,os庫提供了查看文件創建時間的功能。

用到的庫:
  1. os
  2. time
  3. shutil
  4. tkinter
  5. threading
    簡單佈局界面:
    在這裏插入圖片描述

在製作簡單界面時,佈局採用gird函數

        app=Tk()
        app.title('歸檔工具 author:liubingzhe')
        self.path = StringVar()#當前路徑顯示
        self.filename=StringVar()#正在處理的文件名
        self.dicpath=''#當前路徑
        Label(app,text = "選擇路徑:",width='7').grid(row = 0, column = 0)
        Label(app,textvariable =self.filename).grid(row = 1, column = 1)
        Label(app,text ='僅根據創建時間|').grid(row = 2, column = 0)
        Label(app,text ='選擇要處理的文件夾,點擊移動或複製文件').grid(row = 2, column = 1)
        Entry(app, textvariable = self.path,width='35').grid(row = 0, column = 1)
        Button(app, text = "路徑選擇", command = self.selectPath,width='6',height='1').grid(row = 0, column = 3)
        Button(app,text ='複製文件',command =self.thread_run,width='6',height='1').grid(row = 1, column = 3)
        Button(app,text ='移動文件',command =self.thread_run_t,width='6',height='1').grid(row = 2, column = 3)
        app.mainloop()

主體函數:

    def mov_files(self,flag):
        start=time.time()
        dicPath=self.dicpath
        fileList = os.listdir(dicPath)
        for x in fileList:
            filepath = dicPath + "/" + x
            if os.path.isfile(filepath): 
                dataStr = self.get_format_time(os.path.getmtime(filepath))#獲得當前時間
                dirname=os.path.join(dicPath,dataStr)
                if os.path.exists(dirname):
                    self.filename.set('正在移動'+x)
                    shutil.copyfile(filepath,os.path.join(dirname,x)) if flag==1 else shutil.move(filepath,os.path.join(dirname,x))
                else:
                    os.mkdir(dirname)
                    self.filename.set('正在移動'+x)
                    shutil.copyfile(filepath,os.path.join(dirname,x)) if flag==1 else shutil.move(filepath,os.path.join(dirname,x))
        end=time.time()
        self.filename.set('已完成!耗時:%.2f'%(float(end-start)))

加入耗時計算和當前處理文件名
獲取文件創建時間函數:

    def get_format_time(self,timeStamp):
        timeStruct = time.localtime(int(timeStamp))
        return time.strftime("%Y{}%m{}", timeStruct).format('年','月')
        #return time.strftime("%Y{}%m{}%d{}%H{}", timeStruct).format('年','月','日','時')

實現瀏覽文件目錄from tkinter.filedialog import askdirectory
在這裏插入圖片描述

    def selectPath(self):
        self.dicpath = askdirectory()
        self.path.set(self.dicpath)

總代碼:

import os,time,shutil
from tkinter import *
from tkinter.filedialog import askdirectory
import threading
class arrangeGui:
    def __init__(self):
        app=Tk()
        app.title('歸檔工具 author:liubingzhe')
        self.path = StringVar()
        self.filename=StringVar()
        self.dicpath=''
        Label(app,text = "選擇路徑:",width='7').grid(row = 0, column = 0)
        Label(app,textvariable =self.filename).grid(row = 1, column = 1)
        Label(app,text ='僅根據創建時間|').grid(row = 2, column = 0)
        Label(app,text ='選擇要處理的文件夾,點擊移動或複製文件').grid(row = 2, column = 1)
        Entry(app, textvariable = self.path,width='35').grid(row = 0, column = 1)
        Button(app, text = "路徑選擇", command = self.selectPath,width='6',height='1').grid(row = 0, column = 3)
        Button(app,text ='複製文件',command =self.thread_run,width='6',height='1').grid(row = 1, column = 3)
        Button(app,text ='移動文件',command =self.thread_run_t,width='6',height='1').grid(row = 2, column = 3)
        app.mainloop()
    def mov_files(self,flag):
        start=time.time()
        dicPath=self.dicpath
        fileList = os.listdir(dicPath)
        for x in fileList:
            filepath = dicPath + "/" + x
            if os.path.isfile(filepath): 
                dataStr = self.get_format_time(os.path.getmtime(filepath))#獲得當前時間
                dirname=os.path.join(dicPath,dataStr)
                if os.path.exists(dirname):
                    self.filename.set('正在移動'+x)
                    shutil.copyfile(filepath,os.path.join(dirname,x)) if flag==1 else shutil.move(filepath,os.path.join(dirname,x))
                else:
                    os.mkdir(dirname)
                    self.filename.set('正在移動'+x)
                    shutil.copyfile(filepath,os.path.join(dirname,x)) if flag==1 else shutil.move(filepath,os.path.join(dirname,x))
        end=time.time()
        self.filename.set('已完成!耗時:%.2f'%(float(end-start)))
    def get_format_time(self,timeStamp):
        timeStruct = time.localtime(int(timeStamp))
        return time.strftime("%Y{}%m{}", timeStruct).format('年','月')
        #return time.strftime("%Y{}%m{}%d{}%H{}", timeStruct).format('年','月','日','時')
    def selectPath(self):
        self.dicpath = askdirectory()
        self.path.set(self.dicpath)
    def thread_run(self):
        t1=threading.Thread(target=self.mov_files,args=(1,))
        t1.start()
    def thread_run_t(self):
        t1=threading.Thread(target=self.mov_files,args=(2,))
        t1.start()

當前線程已被tk佔用,所以函數必須要另啓線程來處理。
至此,完成。

試一試

在這裏插入圖片描述
總計455個視頻
精確到小時:
在這裏插入圖片描述
對照片處理:
在這裏插入圖片描述在這裏插入圖片描述
兩千多張照片0.02秒完成,真爽……

最後

可以進一步對文件夾或限制文件類型進行處理,比如同類型歸類等,還有許多可以完善擴展的地方。
打包好的應用
下載地址:https://download.csdn.net/download/qq_44198436/12306292

最最後

感嘆!
小學六年級的小朋友寫的代碼……………………
在這裏插入圖片描述
Python進萬家………………

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章