diff --git a/README.md b/README.md index 80bf798e..0a30679f 100644 --- a/README.md +++ b/README.md @@ -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)) diff --git a/python-standard-library/extension-separator/README.md b/python-standard-library/extension-separator/README.md new file mode 100644 index 00000000..92aa986d --- /dev/null +++ b/python-standard-library/extension-separator/README.md @@ -0,0 +1 @@ +# [How to Organize Files by Extension in Python](https://www.thepythoncode.com/article/organize-files-by-extension-with-python) diff --git a/python-standard-library/extension-separator/extension_separator.py b/python-standard-library/extension-separator/extension_separator.py new file mode 100644 index 00000000..1fc26931 --- /dev/null +++ b/python-standard-library/extension-separator/extension_separator.py @@ -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) \ No newline at end of file