Skip to content

Commit

Permalink
add file organizer tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
x4nth055 committed Nov 18, 2021
1 parent 83ac242 commit 95496ee
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
- [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))
- [How to Delete Emails in Python](https://www.thepythoncode.com/article/deleting-emails-in-python). ([code](python-standard-library/deleting-emails))
- [Daemon Threads in Python](https://www.thepythoncode.com/article/daemon-threads-in-python). ([code](python-standard-library/daemon-thread))
- [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))

- ### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
- [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))
Expand Down
1 change: 1 addition & 0 deletions python-standard-library/extension-separator/README.md
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 python-standard-library/extension-separator/extension_separator.py
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)

0 comments on commit 95496ee

Please sign in to comment.