1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import sys import os from pathlib import Path DIRECTORIES = { "图片": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg", ".heif", ".psd"], "视频": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng", ".qt", ".mpg", ".mpeg", ".3gp", ".mkv"], "文档": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods", ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox", ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt", "pptx",".csv",",pdf"], "压缩文件": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z", ".dmg", ".rar", ".xar", ".zip"], "影音": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3", ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"], "文本": [".txt", ".in", ".out"], "编程": [".py",".html5", ".html", ".htm", ".xhtml",".c",".cpp",".java",".css"], "可执行程序": [".exe"], } FILE_FORMATS = {file_format: directory for directory, file_formats in DIRECTORIES.items() for file_format in file_formats} def auto_organize(dirval): for entry in os.scandir(dirval): if entry.is_dir(): continue file_path = Path(dirval + "\\" + entry.name) file_format = file_path.suffix.lower() if file_format in FILE_FORMATS: directory_path = Path(dirval + "\\" + FILE_FORMATS[file_format]) directory_path.mkdir(exist_ok=True) file_path.rename(directory_path.joinpath(entry.name)) try: os.mkdir(dirval + "\\" +"其他文件") except: pass for dir in os.scandir(dirval): try: if dir.is_dir(): os.rmdir(dir) else: temp = str(Path(dir)).split('\\') path = '\\'.join(temp[:-1]) print(path + '\\其他文件\\' + str(temp[-1])) os.rename(str(Path(dir)), path + '\\其他文件\\' + str(temp[-1])) if __name__ == "__main__": auto_organize(sys.argv[1])
|