It sucks
There are many softwares for organizing the desktop with one click
, but for files in other paths, I also need to organize them, so I thought of using Python to complete this demand. Effect display
picture
、 Video
, Audio
, Documentation
, compressed file
, Common format< /code>,program script
, executable program code> and font files
.
# Nested dictionary composed of different files
file_dict = {
'image': ['jpg','png','gif','webp '],
'video': ['rmvb','mp4','avi' ,'mkv','flv'],
'audio': ['cd'< /span>,'wave','aiff','mpeg','mp3','mpeg-4'],
'document': ['xls','xlsx',< span>'csv','doc','docx','ppt','pptx'< /span>,'pdf','txt'],
'compressed file': ['7z'< /span>,'ace','bz','jar','rar','tar','zip','gz'],
'Common format': ['json','xml','md','ximd'],
' Program script': ['py','java','html','sql' span>,'r','css','cpp','c', 'sas','js','go'],
'Executable program': ['exe','bat','lnk','sys','com' span>],
'font file': ['eot','otf','fon' span>,'font','ttf','ttc','woff', 'woff2']
}
Common format
I need to explain to you, for files that are often used but don’t know which type to put, they are all stored here . Development ideas< /span>
os module< /code>,shutil module
,glob module
, they are used together to process files and folders, it's super awesome!
① Any given file path; ② Obtain all files under the current file path, and obtain the corresponding suffix of each file; ③ Determine whether each file is in the specified embedding Set the dictionary and return the corresponding file classification; ④ Determine whether the folder of each file classification exists. Because a new folder needs to be created to store files in categories; ⑤ copy each file to the corresponding category; < /ul>
# Import related libraries
import os
import glob
import shutil< br>
# Use the input() function to dynamically input the file path to be processed.
path = input('Please enter the file path to be cleaned:')
# Define a file dictionary, different file types, belong to Different folders, a total of 9 categories.
file_dict = {
'image': ['jpg','png', 'gif','webp'],
'video': ['rmvb',' mp4','avi','mkv','flv'],
'audio' : ['cd','wave',< span >'aiff','mpeg','mp3',< /span>'mpeg-4'],
'document': ['xls', 'xlsx','csv','doc','docx','ppt' ,'pptx','pdf','txt'],
span >'jar','rar','tar','zip','gz'< /span>],
'Common format': ['json','xml','md'< /span>,'ximd'],
'program script': ['py','java'< /span>,'html','sql','r','css','cpp','c','sas','js','go' span>],
'Executable program': ['exe','bat','lnk'< /span>,'sys','com'],
'font file': ['eot'< /span>,'otf','fon','font','ttf','ttc','woff','woff2']
}
# define a function, pass Enter the suffix corresponding to each file. Determine whether the file exists in the dictionary file_dict;
# If it exists, return the corresponding folder name; if it does not exist, name the folder as 'unknown category';
# br>def func(suffix):
for name, type_list in file_dict.items():
if suffix.lower () in type_list:
return name
return 'unknown category'
< br># Recursively get all the files and folders under the 'path to file to be processed'.
for file in glob.glob(f'{path}/**/*',recursive=True ):
# Since we are classifying files, we need to select files here.
if os.path.isfile(file):
# Due to the isfile() function, the full path of each file is obtained. Here call the basename() function to get the file name directly;
file_name = os.path.basename(file)
suffix = file_name.split('.') [-1]
# Determine whether 'filename' is in the dictionary.
name = func(suffix)
#print(func(suffix))
# Create corresponding folders according to each file classification .
if not os.path.exists(f'{path}\{name}'):
os.mkdir(f< span >'{path}\{name}')
# Copy the files to their respective folders.
shutil. copy(file,Outlook
< span >The code of this article, Huang has already shared it with everyone. But I didn't make a visual interface
for this code. At the same time, I also hope that everyone canpackage the program
, and then send it to other people directly, and then they can use it directly.Interested students can quickly join our planet < section >3 weeks of zero-based entry provides 10 courses12 interesting practical projects throughout the year, including source code, strong> Reward outstanding Top3 students with books every month Professional Q&A group, Dachang’s teacher-nanny-style teaching If you are not satisfied, Refund at will within three days! One year88, now a discount of 16 yuan Scan code to join, 3 weeks zero-based entry
Recommended reading:
Getting Started: The most complete zero-basics learning Python | Zero-basic learning of Python for 8 months span> | Actual project |Learning Python is the shortcut
Dry goods:Crawl Douban short comments, the movie "Later Us" | Analysis of the best NBA players in 38 years | From high expectations to word of mouth! Tang Detective 3 is disappointing | Laughing at the new Yitian Tulongji | Lantern Riddle Answer King |Using Python to make a large number of sketches of young ladies|Mission Impossible is so popular, I use machine learning to make a mini recommendation system movie
Fun:pinball game | Nine Palaces | beautiful flowers | 200 lines of Python "Everyday Cool Running" game!
AI: A robot that can compose poetry | Colorize pictures | Forecast income | Mission: Impossible is so popular, I use machine learning to make a mini recommendation system movie
Gizmo: Pdf to Word, easy to get tables and watermarks! | Save html web pages as pdf with one click! | Goodbye to PDF extraction fees! | Use 90 lines of code to build the strongest PDF converter, word, PPT, excel, markdown, html one-click conversion | Make a DingTalk cheap air ticket reminder! |60 lines of code made a voice wallpaper switcher every day to see Miss Sister! |
Annual blockbuster copywriting
1). Fuck! Convert Pdf to Word easily with Python !
2). Learning Python is really fragrant! I built a website with 100 lines of code, helped people PS travel pictures, and earned chicken legs
3). The first broadcast exceeded 100 million, and it became popular all over the Internet. I analyzed "Sister Riding the Wind and Waves" and discovered these secrets
4).80 lines of code! Use Python to make a doraemon avatar
5). The 20 python codes you must master are short, concise, and infinitely useful
6).30 Python odd skills collection
7).The 80-page "Rookie Learning Python Selected Dry Goods.pdf" I summarized are all dry goods
8).Goodbye Python! I'm going to learn Go! 2500 words in-depth analysis!
9). Discover a dog licking benefit! This Python crawler artifact is so cool, it automatically downloads pictures of girls
Click to read the original text to see 200 original interesting cases!
Articles are uploaded by users and are for non-commercial browsing only. Posted by: Lomu, please indicate the source: https://www.daogebangong.com/en/articles/detail/I%20wrote%20a%20small%20tool%20in%20Python%20no%20matter%20how%20complicated%20the%20folder%20is%20it%20will%20help%20you%20organize%20it%20in%20minutes.html
评论列表(196条)
测试