Python實現(xiàn)一鍵自動分類管理文件
經(jīng)常雜亂無章的文件夾會讓我們找不到所想要的文件,因此小編特意制作了一個可視化GUI界面
,通過輸入路徑一鍵點擊實現(xiàn)文件分門別類的歸檔。
不同的文件后綴歸類為不同的類別
我們先羅列一下大致有幾類文件,根據(jù)文件的后綴來設(shè)定,大致如下
SUBDIR = { "DOCUMENTS": [".pdf", ".docx", ".txt", ".html"], "AUDIO": [".m4a", ".m4b", ".mp3", ".mp4"], "IMAGES": [".jpg", ".jpeg", ".png", ".gif"], "DataFile": [".csv", ".xlsx"] }
上面所羅列出來的文件后綴并不全面,讀者可以根據(jù)自己的需求往里面添加,可以根據(jù)自己的喜好來進行分文別類,然后我們自定義一個函數(shù),根據(jù)輸入的一個文件后綴來判斷它是屬于哪個類的
def pickDir(value): for category, ekstensi in SUBDIR.items(): for suffix in ekstensi: if suffix == value: return category
例如輸入的是.pdf
返回的則是DOCUMENTS
這個類。我們還需要再自定義一個函數(shù),遍歷當前目錄下的所有文件,獲取眾多文件的后綴,將這些不同后綴的文件分別移入不同類別的文件夾,代碼如下
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
我們再次基礎(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ù)完成,請問是否要退出?", 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
代碼打包成可執(zhí)行文件,操作指令如下
pyinstaller -F -w 文件名.py
部分參數(shù)含義如下:
-F
:表示生成單個可執(zhí)行文件-w
:表示去掉控制臺窗口,這在GUI
界面時時非常有用的-i
:表示可執(zhí)行文件的圖標
到此這篇關(guān)于Python實現(xiàn)一鍵自動分類管理文件的文章就介紹到這了,更多相關(guān)Python管理文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解pandas繪制矩陣散點圖(scatter_matrix)的方法
這篇文章主要介紹了詳解pandas繪制矩陣散點圖(scatter_matrix)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-04-04Python數(shù)據(jù)分析之?Matplotlib?3D圖詳情
本文主要介紹了Python數(shù)據(jù)分析之Matplotlib 3D圖詳情,Matplotlib提供了mpl_toolkits.mplot3d工具包來進行3D圖表的繪制,下文總結(jié)了更多相關(guān)資料,需要的小伙伴可以參考一下2022-05-05Python getsizeof()和getsize()區(qū)分詳解
這篇文章主要介紹了Python getsizeof()和getsize()區(qū)分詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-11-11Python標準庫之數(shù)據(jù)庫 sqlite3
這篇文章主要介紹了Python標準庫的數(shù)據(jù)庫 sqlite3的相關(guān)資料,SQLite是一個輕量級、跨平臺的關(guān)系型數(shù)據(jù)庫。它的核心引擎本身不依賴第三方的軟件,使用它也不需要“安裝”。下面文字將對其簡單介紹,需要的小伙伴可以參考下面文章內(nèi)容2021-09-09python列表倒序的幾種方法(切片、reverse()、reversed())
本文主要介紹了python列表倒序的幾種方法(切片、reverse()、reversed()),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-08-08