首頁  >  文章  >  後端開發  >  基於Python怎麼實現自動化文件整理工具

基於Python怎麼實現自動化文件整理工具

王林
王林轉載
2023-05-13 15:13:251424瀏覽

應用場景

個人文件整理:個人在電腦上儲存了大量的照片、影片和文件文件,這些文件可能分散在不同的資料夾中,使用該程式可以將這些文件整理到不同的資料夾中,並依照文件類型分類,方便管理與尋找。

批次文件處理:需要批次處理某個資料夾中的所有文件,例如將影片檔案轉換為特定格式、將圖片檔案縮小到特定尺寸等。

資料備份:將重要的資料備份到外部儲存設備中,按照文件類型分類存儲,如將照片備份到一個資料夾中、將文件文件備份到另一個資料夾中等。

伺服器檔案整理:對於一個包含大量檔案的伺服器,使用程式可以將檔案整理到對應的資料夾中,方便管理和尋找。

資料清理:清理電腦上不需要的文件,例如清理下載資料夾中的暫存文件、清理垃圾箱等。

日誌處理:將特定類型的日誌檔案整理到不同的資料夾中,方便查看和分析。

原始碼

import os
import shutil
import wx
 
class FileOrganizer(wx.Frame):
    def __init__(self, parent, title):
        super(FileOrganizer, self).__init__(parent, title=title, size=(500, 300))
 
        panel = wx.Panel(self)
        self.current_dir = os.getcwd()
 
        # 创建按钮用来选择文件夹
        select_folder_btn = wx.Button(panel, label="选择文件夹", pos=(10, 10))
        select_folder_btn.Bind(wx.EVT_BUTTON, self.on_select_folder)
 
        # 创建按钮用来开始整理文件夹
        organize_btn = wx.Button(panel, label="整理文件夹", pos=(10, 50))
        organize_btn.Bind(wx.EVT_BUTTON, self.on_organize)
 
        # 创建文本框显示当前文件夹路径
        self.dir_text = wx.StaticText(panel, label=self.current_dir, pos=(10, 100))
 
        self.Show()
 
    def on_select_folder(self, event):
        dlg = wx.DirDialog(self, "选择文件夹", style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            self.current_dir = dlg.GetPath()
            self.dir_text.SetLabel(self.current_dir)
        dlg.Destroy()
 
    def on_organize(self, event):
        # 创建文件夹
        photos_dir = os.path.join(self.current_dir, "photos")
        if not os.path.exists(photos_dir):
            os.makedirs(photos_dir)
 
        documents_dir = os.path.join(self.current_dir, "documents")
        if not os.path.exists(documents_dir):
            os.makedirs(documents_dir)
 
        videos_dir = os.path.join(self.current_dir, "videos")
        if not os.path.exists(videos_dir):
            os.makedirs(videos_dir)
 
        shortcuts_dir = os.path.join(self.current_dir, "shortcuts")
        if not os.path.exists(shortcuts_dir):
            os.makedirs(shortcuts_dir)
 
        # 遍历文件夹
        for filename in os.listdir(self.current_dir):
            filepath = os.path.join(self.current_dir, filename)
            if os.path.isfile(filepath):
                ext = os.path.splitext(filename)[1].lower()
                if ext in (".jpg", ".jpeg", ".png", ".gif"):
                    shutil.move(filepath, os.path.join(photos_dir, filename))
                elif ext in (".doc", ".docx", ".pdf", ".txt"):
                    shutil.move(filepath, os.path.join(documents_dir, filename))
                elif ext in (".mp4", ".avi", ".mov", ".wmv"):
                    shutil.move(filepath, os.path.join(videos_dir, filename))
                elif ext == ".lnk":
                    shutil.move(filepath, os.path.join(shortcuts_dir, filename))
 
        wx.MessageBox("文件夹整理完成!", "提示", wx.OK | wx.ICON_INFORMATION)
 
if __name__ == "__main__":
    app = wx.App()
    FileOrganizer(None, title="文件整理工具")
    app.MainLoop()

原始程式碼說明

在該程式碼中,我們建立了一個wxPython的GUI介面,包含了兩個按鈕和一個文字方塊。點選「選擇資料夾」按鈕可以跳出一個對話框用來選擇需要整理的資料夾,點選「整理資料夾」按鈕可以開始整理資料夾。

首先,我們建立了四個資料夾:photos、documents、videos、shortcuts。如果這些資料夾不存在,我們就使用os.makedirs()函數來建立這些資料夾。

然後,我們使用os.listdir()函數遍歷資料夾中的所有檔案。如果檔案是一個檔案而不是資料夾,我們就取得檔案的副檔名,並根據副檔名將該檔案移到對應的資料夾中。我們使用shutil.move()函數將檔案從原始位置移動到新的位置。

最後,我們使用wx.MessageBox()函數在完成整理後彈出一個提示框。

請注意,程式碼只能處理一級目錄下的文件,如果需要處理子目錄中的文件,則需要使用遞歸函數來實現。

效果如下圖

基於Python怎麼實現自動化文件整理工具

以上是基於Python怎麼實現自動化文件整理工具的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除