This script sorts files from a source directory into various subdirectories of a target directory based on their extensions. The program uses asynchronous programming for efficient processing of large numbers of files simultaneously.
- Asynchronous processing: uses
asyncio
for parallel reading and copying of files - Recursive traversal: ability to process nested folders
- Extension-based sorting: automatically creates subdirectories for each file type
- Duplicate handling: automatically renames files with identical names
- Detailed logging: records all operations and errors to console and log file
- Python 3.7 or higher
- Standard Python library (no additional dependencies required)
- Clone the repository or download the script file:
git clone https://github.com/username/async-file-sorter.git
- Make the script file executable (for Linux/Mac):
chmod +x file_sorter.py
python file_sorter.py -s /path/to/source/folder -d /path/to/destination/folder
-s, --source
: Source folder containing files to sort (required parameter)-d, --destination
: Target folder where sorted files will be placed (required parameter)-r, --recursive
: Process subfolders recursively (optional parameter)
python file_sorter.py -s ~/Downloads -d ~/Sorted
python file_sorter.py -s ~/Desktop/Unsorted -d ~/Desktop/Sorted -r
.
├── file_sorter.py # Main program script
├── file_sorter.log # Log file (created when script runs)
└── README.md # This file
- The script analyzes the specified command-line parameters
- Checks for the existence of the source directory and creates the target directory if it doesn't exist
- Recursively or non-recursively traverses all files in the source directory
- For each file:
- Determines its extension
- Creates the corresponding subfolder in the target directory (if it doesn't already exist)
- Copies the file to the appropriate folder
- Renames the file if a file with the same name already exists
- Logs all operations and errors
- Files without extensions are placed in a
no_extension
subfolder - If a file with the same name already exists in the target subfolder, a unique identifier is added to the filename
The script maintains a detailed operation log. Logs are written to:
- The console (INFO level and above)
- The
file_sorter.log
file (INFO level and above)
- The script copies files rather than moving them, so original files remain in place
- Large files may consume significant memory during copying
- The script checks only file extensions, not their contents