久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

博客專欄

EEPW首頁 > 博客 > 用 Python 制作可視化 GUI 界面,一鍵實現自動分類管理文件!

用 Python 制作可視化 GUI 界面,一鍵實現自動分類管理文件!

發布人:AI科技大本營 時間:2022-05-15 來源:工程師 發布文章

以下文章來源于Python愛好者集中營 ,作者欣一

作者 | 欣一  

來源 | Python愛好者集中營

經常雜亂無章的文件夾會讓我們找不到所想要的文件,因此小編特意制作了一個可視化GUI界面,通過輸入路徑一鍵點擊實現文件分門別類的歸檔。

不同的文件后綴歸類為不同的類別

我們先羅列一下大致有幾類文件,根據文件的后綴來設定,大致如下:

SUBDIR = {
    "DOCUMENTS": [".pdf"".docx"".txt"".html"],
    "AUDIO": [".m4a"".m4b"".mp3"".mp4"],
    "IMAGES": [".jpg"".jpeg"".png"".gif"],
    "DataFile": [".csv"".xlsx"]
}

上面所羅列出來的文件后綴并不全面,讀者可以根據自己的需求往里面添加,可以根據自己的喜好來進行分文別類,然后我們自定義一個函數,根據輸入的一個文件后綴來判斷它是屬于哪個類的。

def pickDir(value):
    for category, ekstensi in SUBDIR.items():
        for suffix in ekstensi:
            if suffix == value:
                return category

例如輸入的是.pdf返回的則是DOCUMENTS這個類。我們還需要再自定義一個函數,遍歷當前目錄下的所有文件,獲取眾多文件的后綴,將這些不同后綴的文件分別移入不同類別的文件夾,代碼如下:

def organizeDir(path_val):

    for item in os.scandir(path_val):
        if item.is_dir():
            continue

        filePath = Path(item)
        file_suffix = filePath.suffix.lower()
        directory = pickDir(file_suffix)
        directoryPath = Path(directory)
        # 新建文件夾,要是該文件夾不存在的話
        if directoryPath.is_dir() != True:
            directoryPath.mkdir()
        filePath.rename(directoryPath.joinpath(filePath))

output

圖片

我們再次基礎之上,再封裝一下做成Python可視化GUI界面,代碼如下:

class FileOrgnizer(QWidget):
    def __init__(self):
        super().__init__()
        self.lb = QLabel(self)
        self.lb.setGeometry(70, 25, 80, 40)
        self.lb.setText('文件夾整理助手:')
        self.textbox = QLineEdit(self)
        self.textbox.setGeometry(170, 30, 130, 30)
        self.findButton = QPushButton('整理', self)
        self.findButton.setGeometry(60, 85, 100, 40)
        self.quitButton = QPushButton('退出', self)
        self.quitButton.clicked.connect(self.closeEvent)
        self.findButton.clicked.connect(self.organizeDir)
        self.quitButton.setGeometry(190, 85, 100, 40)
        self.setGeometry(500, 500, 350, 150)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('../751.png'))
        self.show()

    def pickDir(self, value):
        for category, ekstensi in SUBDIR.items():
            for suffix in ekstensi:
                if suffix == value:
                    return category

    def organizeDir(self, event):

        path_val = self.textbox.text()
        print("路徑為: " + path_val)
        for item in os.scandir(path_val):
            if item.is_dir():
                continue

            filePath = Path(item)
            fileType = filePath.suffix.lower()
            directory = self.pickDir(fileType)
            if directory == None:
                continue

            directoryPath = Path(directory)
            if directoryPath.is_dir() != True:
                directoryPath.mkdir()
            filePath.rename(directoryPath.joinpath(filePath))
        reply = QMessageBox.information(self, "完成""任務完成,請問是否要退出?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    def closeEvent(self, event):
        reply = QMessageBox.question(self, '退出',
                                     "確定退出?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

效果如下圖所示

圖片

最后我們通過pyinstaller模塊來將Python代碼打包成可執行文件,操作指令如下

pyinstaller -F -w 文件名.py

部分參數含義如下:

  • -F:表示生成單個可執行文件

  • -w:表示去掉控制臺窗口,這在GUI界面時時非常有用的

  • -i:表示可執行文件的圖標


*博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



關鍵詞: 算法

相關推薦

技術專區

關閉