forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# [How to Organize Files by Extension in Python](https://www.thepythoncode.com/article/organize-files-by-extension-with-python) |
65 changes: 65 additions & 0 deletions
65
python-standard-library/extension-separator/extension_separator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
import glob | ||
import shutil | ||
|
||
# dictionary mapping each extension with its corresponding folder | ||
# For example, 'jpg', 'png', 'ico', 'gif', 'svg' files will be moved to 'images' folder | ||
# feel free to change based on your needs | ||
extensions = { | ||
"jpg": "images", | ||
"png": "images", | ||
"ico": "images", | ||
"gif": "images", | ||
"svg": "images", | ||
"sql": "sql", | ||
"exe": "programs", | ||
"msi": "programs", | ||
"pdf": "pdf", | ||
"xlsx": "excel", | ||
"csv": "excel", | ||
"rar": "archive", | ||
"zip": "archive", | ||
"gz": "archive", | ||
"tar": "archive", | ||
"docx": "word", | ||
"torrent": "torrent", | ||
"txt": "text", | ||
"ipynb": "python", | ||
"py": "python", | ||
"pptx": "powerpoint", | ||
"ppt": "powerpoint", | ||
"mp3": "audio", | ||
"wav": "audio", | ||
"mp4": "video", | ||
"m3u8": "video", | ||
"webm": "video", | ||
"ts": "video", | ||
"json": "json", | ||
"css": "web", | ||
"js": "web", | ||
"html": "web", | ||
"apk": "apk", | ||
"sqlite3": "sqlite3", | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
path = r"E:\Downloads" | ||
# setting verbose to 1 (or True) will show all file moves | ||
# setting verbose to 0 (or False) will show basic necessary info | ||
verbose = 0 | ||
for extension, folder_name in extensions.items(): | ||
# get all the files matching the extension | ||
files = glob.glob(os.path.join(path, f"*.{extension}")) | ||
print(f"[*] Found {len(files)} files with {extension} extension") | ||
if not os.path.isdir(os.path.join(path, folder_name)) and files: | ||
# create the folder if it does not exist before | ||
print(f"[+] Making {folder_name} folder") | ||
os.mkdir(os.path.join(path, folder_name)) | ||
for file in files: | ||
# for each file in that extension, move it to the correponding folder | ||
basename = os.path.basename(file) | ||
dst = os.path.join(path, folder_name, basename) | ||
if verbose: | ||
print(f"[*] Moving {file} to {dst}") | ||
shutil.move(file, dst) |