Table of Contents
Too bad
Effect display
Development Ideas
Outlook
Home Backend Development Python Tutorial I wrote a small tool in Python to help you organize even complex folders in minutes!

I wrote a small tool in Python to help you organize even complex folders in minutes!

Apr 11, 2023 pm 08:19 PM
python tool code

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.

I wrote a small tool in Python to help you organize even complex folders in minutes!

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']
}
Copy after login

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}")
Copy after login

The results are as follows:

I wrote a small tool in Python to help you organize even complex folders in minutes!

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China May 08, 2025 pm 10:51 PM

In the cryptocurrency market, choosing a reliable trading platform is crucial. As a world-renowned digital asset exchange, the OK trading platform has attracted a large number of novice users in mainland China. This guide will introduce in detail how to register and use it on the OK trading platform to help novice users get started quickly.

Top 10 cryptocurrency platforms in the world that support multi-chain transactions are authoritatively released in 2025 Top 10 cryptocurrency platforms in the world that support multi-chain transactions are authoritatively released in 2025 May 08, 2025 pm 07:15 PM

According to the latest evaluations and industry trends from authoritative institutions in 2025, the following are the top ten cryptocurrency platforms in the world that support multi-chain transactions, combining transaction volume, technological innovation, compliance and user reputation comprehensive analysis:

TOP10 futures trading platforms: Perpetual contracts and options trading TOP10 futures trading platforms: Perpetual contracts and options trading May 08, 2025 pm 07:12 PM

In the cryptocurrency market, futures trading platforms play an important role, especially in perpetual contracts and options trading. Here are the top ten highly respected futures trading platforms in the market, and provide detailed introduction to their characteristics and advantages in perpetual contract and option trading.

Python vs. JavaScript: A Comparative Analysis for Developers Python vs. JavaScript: A Comparative Analysis for Developers May 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Top 10 cryptocurrency exchanges in the currency circle, the latest ranking of the top 10 digital currency trading platforms in 2025 Top 10 cryptocurrency exchanges in the currency circle, the latest ranking of the top 10 digital currency trading platforms in 2025 May 08, 2025 pm 10:45 PM

Ranking of the top ten cryptocurrency exchanges in the currency circle: 1. Binance: Leading the world, providing efficient trading and a variety of financial products. 2. OKX: It is innovative and diverse, supporting a variety of transaction types. 3. Huobi: Stable and reliable, with high-quality service. 4. Coinbase: Be friendly for beginners and simple interface. 5. Kraken: The first choice for professional traders, with powerful tools. 6. Bitfinex: efficient trading, rich trading pairs. 7. Bittrex: Safety compliance, regulatory cooperation. 8. Poloniex and so on.

Ranking of the top ten digital currency quantitative trading apps, the latest list of digital currency exchanges Ranking of the top ten digital currency quantitative trading apps, the latest list of digital currency exchanges May 08, 2025 pm 06:03 PM

Ranking of the top ten digital currency quantitative trading apps: 1. Binance, 2. OKX, 3. Huobi, 4. Coinbase, 5. Kraken, 6. Bitfinex, 7. Bittrex, 8. Poloniex, 9. Gemini, 10. KuCoin, these platforms provide high security and good user experience, and the steps to use include downloading and installing, registering an account, enabling two-step verification, and depositing and trading.

2025 latest! Ranking of the top ten most popular formal virtual currency trading apps 2025 latest! Ranking of the top ten most popular formal virtual currency trading apps May 08, 2025 pm 07:48 PM

In 2025, the virtual currency market is still hot, and investors are constantly looking for the best trading platform. This article will introduce the top ten most popular formal virtual currency trading apps in 2025 in detail, and provide their rankings and features to help you make the wisest choices.

Does Binance have a mobile app? Binance binance official website mobile version Android APP recommendation Does Binance have a mobile app? Binance binance official website mobile version Android APP recommendation May 08, 2025 pm 10:12 PM

Binance, as the world's leading cryptocurrency trading platform, provides a variety of ways for users to trade and manage assets easily. Among them, Binance Mobile APP is one of the tools chosen by many users. The following details the download and use of Binance's official Android APP.

See all articles