Status: Complete | Language: Python 3.x | Category: Automation/Utility |
---|
This is a single-script Python utility designed to automatically organize the contents of any specified directory into subfolders based on file type (e.g., Images, Documents, Code). It demonstrates practical scripting, robust I/O handling, and essential file system management.
- File System Automation (I/O): Utilizes Python's
os
andshutil
libraries for low-level file path manipulation (os.path.join
,os.makedirs
) and reliable file movement (shutil.move
). - Data-Driven Configuration: The organization rules are defined entirely within a configurable
file_types
dictionary, allowing for easy expansion and maintenance. - Defensive Programming: Implements
try...except
blocks to handle common runtime errors, such as invalid directory paths and file permission errors, ensuring the script is robust. - Path Manipulation: Uses
os.path.splitext
and string methods to accurately parse file extensions and determine the correct destination.
The script follows a clear, two-stage process:
- Preparation:
- Accepts a directory path from the user.
- Creates all necessary destination folders (
Images
,Documents
, etc.) usingos.makedirs(..., exist_ok=True)
.
- Execution:
- Iterates through every item in the source directory using
os.listdir()
. - Bypasses directories (
os.path.isdir(...)
). - Matches the file extension against the
file_types
dictionary. - Moves the file to the corresponding folder (or to a
Misc
folder if the type is unknown) usingshutil.move()
.
- Iterates through every item in the source directory using
- Python 3.x (No external libraries required.)
- Save the code as
file_organizer.py
. - Open your terminal or command prompt.
- Run the script:
python file_organizer.py
- The script will prompt you to enter the full path of the directory you wish to organize.