Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit95496ee

Browse files
committed
add file organizer tutorial
1 parent83ac242 commit95496ee

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
129129
-[How to Make a Chat Application in Python](https://www.thepythoncode.com/article/make-a-chat-room-application-in-python). ([code](python-standard-library/chat-application))
130130
-[How to Delete Emails in Python](https://www.thepythoncode.com/article/deleting-emails-in-python). ([code](python-standard-library/deleting-emails))
131131
-[Daemon Threads in Python](https://www.thepythoncode.com/article/daemon-threads-in-python). ([code](python-standard-library/daemon-thread))
132+
-[How to Organize Files by Extension in Python](https://www.thepythoncode.com/article/organize-files-by-extension-with-python). ([code](python-standard-library/extension-separator))
132133

133134
-###[Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
134135
-[How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Organize Files by Extension in Python](https://www.thepythoncode.com/article/organize-files-by-extension-with-python)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
importos
2+
importglob
3+
importshutil
4+
5+
# dictionary mapping each extension with its corresponding folder
6+
# For example, 'jpg', 'png', 'ico', 'gif', 'svg' files will be moved to 'images' folder
7+
# feel free to change based on your needs
8+
extensions= {
9+
"jpg":"images",
10+
"png":"images",
11+
"ico":"images",
12+
"gif":"images",
13+
"svg":"images",
14+
"sql":"sql",
15+
"exe":"programs",
16+
"msi":"programs",
17+
"pdf":"pdf",
18+
"xlsx":"excel",
19+
"csv":"excel",
20+
"rar":"archive",
21+
"zip":"archive",
22+
"gz":"archive",
23+
"tar":"archive",
24+
"docx":"word",
25+
"torrent":"torrent",
26+
"txt":"text",
27+
"ipynb":"python",
28+
"py":"python",
29+
"pptx":"powerpoint",
30+
"ppt":"powerpoint",
31+
"mp3":"audio",
32+
"wav":"audio",
33+
"mp4":"video",
34+
"m3u8":"video",
35+
"webm":"video",
36+
"ts":"video",
37+
"json":"json",
38+
"css":"web",
39+
"js":"web",
40+
"html":"web",
41+
"apk":"apk",
42+
"sqlite3":"sqlite3",
43+
}
44+
45+
46+
if__name__=="__main__":
47+
path=r"E:\Downloads"
48+
# setting verbose to 1 (or True) will show all file moves
49+
# setting verbose to 0 (or False) will show basic necessary info
50+
verbose=0
51+
forextension,folder_nameinextensions.items():
52+
# get all the files matching the extension
53+
files=glob.glob(os.path.join(path,f"*.{extension}"))
54+
print(f"[*] Found{len(files)} files with{extension} extension")
55+
ifnotos.path.isdir(os.path.join(path,folder_name))andfiles:
56+
# create the folder if it does not exist before
57+
print(f"[+] Making{folder_name} folder")
58+
os.mkdir(os.path.join(path,folder_name))
59+
forfileinfiles:
60+
# for each file in that extension, move it to the correponding folder
61+
basename=os.path.basename(file)
62+
dst=os.path.join(path,folder_name,basename)
63+
ifverbose:
64+
print(f"[*] Moving{file} to{dst}")
65+
shutil.move(file,dst)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp