


I wrote a small tool in Python to help you organize even complex folders in minutes!
Too bad
I admit that I am not a person who likes to tidy up my desktop, because I think a messy desktop makes it easier to find files.
Haha, but the desktop has been so messy recently that I can’t even see it anymore. It almost takes up the entire screen. Although there are many software to organize the desktop with one click, I also need to organize files in other paths, so I thought of using Python to complete this requirement.
Effect display
I divided the files into 9 major categories, namely pictures, videos, audios, documents, compressed files, and common formats , program scripts, executable programs and font files.
# 不同文件组成的嵌套字典 file_dict = { '图片': ['jpg','png','gif','webp'], '视频': ['rmvb','mp4','avi','mkv','flv'], "音频": ['cd','wave','aiff','mpeg','mp3','mpeg-4'], '文档': ['xls','xlsx','csv','doc','docx','ppt','pptx','pdf','txt'], '压缩文件': ['7z','ace','bz','jar','rar','tar','zip','gz'], '常用格式': ['json','xml','md','ximd'], '程序脚本': ['py','java','html','sql','r','css','cpp','c','sas','js','go'], '可执行程序': ['exe','bat','lnk','sys','com'], '字体文件': ['eot','otf','fon','font','ttf','ttc','woff','woff2'] }
file_dict is a dictionary defined by ourselves, which contains formats commonly used in our study and work. The commonly used formats need to be explained to everyone. For files that are often used but don’t know which category to put them in, they are stored here.
Note: If your computer has more file formats, you only need to modify the above file_dict dictionary.
Development Ideas
Developing such a small tool involves a total of three Python libraries, namely the os module, shutil module, and glob module. They are used together to process files and folders. , simply awesome!
The general idea of the entire development steps is as follows:
- ① Given a file path arbitrarily;
- ② Get all the files under the current file path, and Get the suffix corresponding to each file;
- ③ Determine whether each file is in the specified nested dictionary, and return the corresponding file classification;
- ④ Determine the file of each file classification Whether the folder exists. Because it is necessary to create a new folder to store files in categories;
- ⑤ Copy each file to the corresponding category;
The complete code is as follows: [Attached is the detailed code Remarks]
# 导入相关库 import os import glob import shutil # 采用input()函数,动态输入要处理的文件路径。 path = input("请输入要清理的文件路径:") # 定义一个文件字典,不同的文件类型,属于不同的文件夹,一共9个大类。 file_dict = { '图片': ['jpg','png','gif','webp'], '视频': ['rmvb','mp4','avi','mkv','flv'], "音频": ['cd','wave','aiff','mpeg','mp3','mpeg-4'], '文档': ['xls','xlsx','csv','doc','docx','ppt','pptx','pdf','txt'], '压缩文件': ['7z','ace','bz','jar','rar','tar','zip','gz'], '常用格式': ['json','xml','md','ximd'], '程序脚本': ['py','java','html','sql','r','css','cpp','c','sas','js','go'], '可执行程序': ['exe','bat','lnk','sys','com'], '字体文件': ['eot','otf','fon','font','ttf','ttc','woff','woff2'] } # 定义一个函数,传入每个文件对应的后缀。判断文件是否存在于字典file_dict中; # 如果存在,返回对应的文件夹名;如果不存在,将该文件夹命名为"未知分类"; def func(suffix): for name, type_list in file_dict.items(): if suffix.lower() in type_list: return name return "未知分类" # 递归获取 "待处理文件路径" 下的所有文件和文件夹。 for file in glob.glob(f"{path}/**/*",recursive=True): # 由于我们是对文件分类,这里需要挑选出文件来。 if os.path.isfile(file): # 由于isfile()函数,获取的是每个文件的全路径。这里再调用basename()函数,直接获取文件名; file_name = os.path.basename(file) suffix = file_name.split(".")[-1] # 判断 "文件名" 是否在字典中。 name = func(suffix) #print(func(suffix)) # 根据每个文件分类,创建各自对应的文件夹。 if not os.path.exists(f"{path}\{name}"): os.mkdir(f"{path}\{name}") # 将文件复制到各自对应的文件夹中。 shutil.copy(file,f"{path}\{name}")
The results are as follows:
Outlook
Huang has shared the code of this article with everyone. But I didn't make a visual interface for this code. At the same time, I also hope that everyone can package the program and send it directly to others so that they can use it directly.
The above is the detailed content of I wrote a small tool in Python to help you organize even complex folders in minutes!. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software