Tips & GuidesPython File Sorting Script
Python File Sorting Script
Script to automatically sort school subject files into directories by keyword matching.
Last updated: January 1, 2024
A Python script that scans files in a directory and sorts them into subject-specific folders based on keyword matching.
How It Works
The script reads filenames and checks for subject keywords (Biology, Chemistry, Physics, Maths, etc.). It then moves each file into the corresponding folder.
Script
pythonimport os import shutil # Define subject folders and their keywords subject_map = { "Biology": ["bio", "biology"], "Catechism": ["catechism", "cate"], "Chemistry": ["chem", "chemistry"], "Computer Science": ["computer", "cs", "comp"], "English": ["english", "eng"], "General": ["general"], "Maths": ["maths", "math", "mathematics"], "Physics": ["physics", "phy"], } source_dir = "." # change to your target folder for filename in os.listdir(source_dir): if os.path.isfile(os.path.join(source_dir, filename)): name_lower = filename.lower() for subject, keywords in subject_map.items(): if any(kw in name_lower for kw in keywords): dest_dir = os.path.join(source_dir, subject) os.makedirs(dest_dir, exist_ok=True) shutil.move( os.path.join(source_dir, filename), os.path.join(dest_dir, filename) ) print(f"Moved {filename} -> {subject}/") break
Usage
- Save the script as
sort_files.pyin the folder containing your unsorted files - Update the
subject_mapwith your own subjects and keywords - Run
python sort_files.py
Was this helpful?