博客專欄

        EEPW首頁(yè) > 博客 > 用 Python 制作可視化 GUI 界面,一鍵實(shí)現(xiàn)自動(dòng)分類管理文件!

        用 Python 制作可視化 GUI 界面,一鍵實(shí)現(xiàn)自動(dòng)分類管理文件!

        發(fā)布人:AI科技大本營(yíng) 時(shí)間:2022-05-15 來(lái)源:工程師 發(fā)布文章

        以下文章來(lái)源于Python愛(ài)好者集中營(yíng) ,作者欣一

        作者 | 欣一  

        來(lái)源 | Python愛(ài)好者集中營(yíng)

        經(jīng)常雜亂無(wú)章的文件夾會(huì)讓我們找不到所想要的文件,因此小編特意制作了一個(gè)可視化GUI界面,通過(guò)輸入路徑一鍵點(diǎn)擊實(shí)現(xiàn)文件分門(mén)別類的歸檔。

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

        我們先羅列一下大致有幾類文件,根據(jù)文件的后綴來(lái)設(shè)定,大致如下:

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

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

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

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

        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

        圖片

        我們?cè)俅位A(chǔ)之上,再封裝一下做成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, "完成""任務(wù)完成,請(qǐng)問(wèn)是否要退出?", 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()

        效果如下圖所示

        圖片

        最后我們通過(guò)pyinstaller模塊來(lái)將Python代碼打包成可執(zhí)行文件,操作指令如下

        pyinstaller -F -w 文件名.py

        部分參數(shù)含義如下:

        • -F:表示生成單個(gè)可執(zhí)行文件

        • -w:表示去掉控制臺(tái)窗口,這在GUI界面時(shí)時(shí)非常有用的

        • -i:表示可執(zhí)行文件的圖標(biāo)


        *博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。



        關(guān)鍵詞: 算法

        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 府谷县| 潞西市| 松潘县| 景宁| 隆德县| 宁南县| 穆棱市| 藁城市| 长垣县| 上栗县| 扶风县| 大同县| 沁阳市| 梁山县| 青川县| 重庆市| 临泽县| 木兰县| 镶黄旗| 克山县| 特克斯县| 汉川市| 宜兴市| 德令哈市| 长顺县| 资阳市| 望江县| 怀安县| 福泉市| 嘉定区| 鄯善县| 南京市| 县级市| 灌南县| 万源市| 神木县| 宽城| 西青区| 蓬莱市| 江安县| 左贡县|