


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!

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Version control is a key link when managing PHP projects. Recently I was working on a Git-based PHP project and I encountered a problem: how to automatically generate and manage version numbers during development. This problem seems simple, but manual maintenance of the version number is not only cumbersome, but also prone to errors. After some exploration, I found a very useful tool - the sebastian/version library, which was easily integrated into the project through Composer, completely solving my troubles.
