When using a computer daily, one often encounters the situation of a “messy Downloads folder”—
PDFs, images, videos, and compressed files all mixed together. Manually dragging and sorting them one by one is too inefficient.
In fact, you can write a small script in Python to automatically classify and move files to designated folders based on rules such as file type, date, size, etc.
1. Basic Approach
- 1. First, scan the target directory (for example, the Downloads folder).
- 2. Determine the category of each file based on the rules.
- 3. Create a corresponding folder if it does not exist.
- 4. Move the files to the designated folders.
Core tools: <span>pathlib</span> for handling paths, <span>shutil.move</span> for moving files.
2. Classifying by File Type
The most common method is to classify by file extension:
from pathlib import Path
import shutil
src = Path(r"D:\Downloads")
dst = Path(r"D:\Sorted")
rules = {
"Images": [".jpg", ".png", ".gif"],
"Documents": [".pdf", ".docx", ".txt"],
"Compressed": [".zip", ".rar", ".7z"],
"Videos": [".mp4", ".avi", ".mkv"],
}
for file in src.iterdir():
if file.is_file():
ext = file.suffix.lower()
for folder, exts in rules.items():
if ext in exts:
target = dst / folder
target.mkdir(parents=True, exist_ok=True)
shutil.move(str(file), str(target / file.name))
print(f"Moved: {file.name} -> {folder}")
After execution, the files in the Downloads folder will automatically move to their corresponding categorized folders.
3. Classifying by Date
Sometimes, it is preferable to organize files by date, such as sorting photos by year and month:
from pathlib import Path
import shutil
import datetime
src = Path(r"D:\Photos")
dst = Path(r"D:\Photos_Sorted")
for file in src.iterdir():
if file.is_file():
mtime = datetime.datetime.fromtimestamp(file.stat().st_mtime)
folder = f"{mtime.year}-{mtime.month:02d}"
target = dst / folder
target.mkdir(parents=True, exist_ok=True)
shutil.move(str(file), str(target / file.name))
print(f"{file.name} -> {folder}")
Thus, photos taken in May 2023 will be uniformly placed in the <span>2023-05</span> folder.
4. Classifying by Size
For example, you may want to separate large files from small files:
from pathlib import Path
import shutil
src = Path(r"D:\Downloads")
dst = Path(r"D:\Sorted")
for file in src.iterdir():
if file.is_file():
size = file.stat().st_size # bytes
if size > 50 * 1024 * 1024: # greater than 50MB
folder = "Large Files"
else:
folder = "Small Files"
target = dst / folder
target.mkdir(parents=True, exist_ok=True)
shutil.move(str(file), str(target / file.name))
print(f"{file.name} -> {folder}")
5. Mixed Rules
For instance: first classify by file type, then organize by date. You can first create type folders, and then create subfolders by month within those type folders. The logic is two layers of <span>mkdir</span> + <span>shutil.move</span>.
6. Considerations
- 1. Test before running in bulk: You can first use
<span>print</span>to display the target paths, confirming there are no issues before actually moving files. - 2. Duplicate Files: If there are already files with the same name in the target folder,
<span>shutil.move</span>will throw an error; you can add logic to automatically rename them. - 3. Do not modify system folders: Only organize directories you use, such as Downloads, Documents, and Photos; do not tamper with system files in the C drive.
- 4. Backup: If the files are important, it is best to make a copy before organizing.
With this script, the Downloads folder will no longer be a “garbage heap”. With one command, everything is instantly organized!