Home  >  Article  >  Backend Development  >  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!

WBOY
WBOYforward
2023-04-11 20:19:181489browse

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']
}

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:

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:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete