Skip to content

Commit 95496ee

Browse files
committed
add file organizer tutorial
1 parent 83ac242 commit 95496ee

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

+1
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))
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import glob
3+
import shutil
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+
for extension, folder_name in extensions.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+
if not os.path.isdir(os.path.join(path, folder_name)) and files:
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+
for file in files:
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+
if verbose:
64+
print(f"[*] Moving {file} to {dst}")
65+
shutil.move(file, dst)

0 commit comments

Comments
 (0)