Python 中的无用文件整理器?
对于一个懒惰的 python 程序员来说,这似乎非常有用,他把大多数文件和文件夹都放在同一个位置,有时会混淆哪些文件在那里,而且他肯定太懒了,不愿意手动整理。因此,以下是一个 python 程序,它可以一次性将所有内容整理或简化到适当的文件夹中,并删除空目录。
因此,我们有一个目录路径,其中存在许多不同类型的文件(如下所示),我们的程序会将每种文件类型分类到各自的文件夹中(如下所示)。
输入文件夹结构
期望的输出
首先根据我们将要分类到不同文件夹中的文件类型创建不同的文件夹
DIRECTORIES = { "HTML": [".html5", ".html", ".htm", ".xhtml"], "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg", ".heif", ".psd"], "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng", ".qt", ".mpg", ".mpeg", ".3gp"], "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods", ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox", ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt", "pptx"], "ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z", ".dmg", ".rar", ".xar", ".zip"], "AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3", ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"], "PLAINTEXT": [".txt", ".in", ".out"], "PDF": [".pdf"], "PYTHON": [".py"], "XML": [".xml"], "EXE": [".exe"], "SHELL": [".sh"] }
其次,创建文件类型到各自文件夹的映射
FILE_FORMATS = {file_format: directory for directory, file_formats in DIRECTORIES.items() for file_format in file_formats} def organise_folder(): for entry in os.scandir(): if entry.is_dir(): continue file_path = Path(entry) file_format = file_path.suffix.lower() if file_format in FILE_FORMATS: directory_path = Path(FILE_FORMATS[file_format]) directory_path.mkdir(exist_ok=True) file_path.rename(directory_path.joinpath(file_path)) try: os.mkdir("OTHER-FILES") except: pass for dir in os.scandir(): try: if dir.is_dir(): os.rmdir(dir) else: os.rename(os.getcwd() + '/' + str(Path(dir)), os.getcwd() + '/OTHER-FILES/' + str(Path(dir))) except: pass
最终脚本
就是这样,以下是我们将文件类型筛选到各自文件夹中的最终脚本。
#Python Lazy Junk Files Organizer #Import important libraries import os from pathlib import Path # DIRECTORIES = { "HTML": [".html5", ".html", ".htm", ".xhtml"], "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg", ".heif", ".psd"], "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng", ".qt", ".mpg", ".mpeg", ".3gp"], "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods", ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox", ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt", "pptx"], "ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z", ".dmg", ".rar", ".xar", ".zip"], "AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3", ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"], "PLAINTEXT": [".txt", ".in", ".out"], "PDF": [".pdf"], "PYTHON": [".py"], "XML": [".xml"], "EXE": [".exe"], "SHELL": [".sh"] } FILE_FORMATS = {file_format: directory for directory, file_formats in DIRECTORIES.items() for file_format in file_formats} def organise_folder(): for entry in os.scandir(): if entry.is_dir(): continue file_path = Path(entry) file_format = file_path.suffix.lower() if file_format in FILE_FORMATS: directory_path = Path(FILE_FORMATS[file_format]) directory_path.mkdir(exist_ok=True) file_path.rename(directory_path.joinpath(file_path)) try: os.mkdir("OTHER-FILES") except: pass for dir in os.scandir(): try: if dir.is_dir(): os.rmdir(dir) else: os.rename(os.getcwd() + '/' + str(Path(dir)), os.getcwd() + '/OTHER-FILES/' + str(Path(dir))) except: pass if __name__ == "__main__": organise_folder()
从某个特定目录路径运行上述脚本后,我们将获得类似以下内容的输出:
输出
广告