Before we get started, have you tried our new Python Code Assistant? It's like having an expert coder at your fingertips. Check it out!
Organizing files on your computer can be a tiring task especially when you do it manually, some folders (such as the Downloads folder) in your computer are too messy that you can't even search there. In this tutorial, you will get involved with Python to organize files by extension in any folder on your computer.
Without further ado, let's get started. We don't need any library to install, as we'll be using the Python standard library. Importing them:
import os
import glob
import shutil
We'll be using the shutil.move()
function from shutil module to move files, and we need glob as well to get a list of matched files using regex. We also use the os module for handling file names.
Since we're going to organize files by extension, it totally makes sense to put the files of the same type (such as mp3
, wav
) on the same folder (such as "audio"
), below Python dictionary maps each extension to a type, feel free to edit on your needs:
# 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",
}
For example, mp4
and m3u8
files will be moved to a folder called video
, JSON files will have their own folder, etc. This is not a complete list of extensions, so you have to add more in your case.
Let's get into the code now:
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)
We're iterating over our dictionary that maps each extension to a file type, we get the list of files of each extension and move them to the corresponding folder. We also create the folder using os.mkdir()
function if it does not exist before.
The path
variable is the path of the folder you want to organize, you can use sys or argparse modules to pass the folder path in the command-line arguments.
This is a part of my Downloads folder:
Too messy, files with different types and if I want to search for a specific file but don't know the exact name, I would need to scroll a lot.
Let's run it:
$ python extension_separator.py
Output:
[*] Found 17 files with jpg extension
[+] Making images folder
[*] Found 6 files with png extension
[*] Found 0 files with ico extension
[*] Found 0 files with gif extension
[*] Found 0 files with svg extension
[*] Found 3 files with sql extension
[+] Making sql folder
[*] Found 0 files with exe extension
[*] Found 3 files with msi extension
[+] Making programs folder
[*] Found 38 files with pdf extension
[+] Making pdf folder
[*] Found 7 files with xlsx extension
[+] Making excel folder
[*] Found 9 files with csv extension
[*] Found 2 files with rar extension
[+] Making archive folder
[*] Found 9 files with zip extension
[*] Found 2 files with gz extension
[*] Found 0 files with tar extension
[*] Found 5 files with docx extension
[+] Making word folder
[*] Found 11 files with torrent extension
[+] Making torrent folder
[*] Found 2 files with txt extension
[+] Making text folder
[*] Found 5 files with ipynb extension
[+] Making python folder
[*] Found 4 files with py extension
[*] Found 0 files with pptx extension
[*] Found 0 files with ppt extension
[*] Found 0 files with mp3 extension
[*] Found 0 files with wav extension
[*] Found 1 files with mp4 extension
[+] Making video folder
[*] Found 0 files with m3u8 extension
[*] Found 1 files with webm extension
[*] Found 0 files with ts extension
[*] Found 11 files with json extension
[+] Making json folder
[*] Found 0 files with css extension
[*] Found 0 files with js extension
[*] Found 0 files with html extension
[*] Found 0 files with apk extension
[*] Found 0 files with sqlite3 extension
If you set verbose
to 1, you'll get all moving files in the output. This is the folder after running the Python script:
Now I know if I want to search for a file, I simply go to its type and will find it there, or if I want to delete useless files, it's much easier now.
Alright, that's it for this tutorial, I hope it was a helpful and handy code for you to organize your folders!
Check the full code here.
Learn also: How to Handle Files in Python
Happy coding ♦
Loved the article? You'll love our Code Converter even more! It's your secret weapon for effortless coding. Give it a whirl!
View Full Code Transform My Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!