Skip to content →

Sort Files in a Directory by File-Extension using Python

Here is a python script that sorts files in a directory by file extension.
The directory to be sorted and the file endings to be sorted are taken as input parameters.
As an example, the script can be immensely helpful in creating order in a download folder that has grown and grown over time.

This script first checks if the specified directory is valid. If it is, it iterates through all the files in the directory and checks their file extensions. If the file extension is one of the file endings specified in the input parameters, the file is moved to a sub-folder named after the file extension. The subfolder is created if it doesn’t exist yet.

This script keeps track of the number of files moved for each file extension in the files_moved dictionary. It also keeps track of the number of files not moved in the directory in the files_not_moved variable. At the end of the script, it prints out the number of files moved per file extension and the number of files not moved in the directory.

You can download the sourcecode from my github repository:

https://github.com/smartDevel/fileSorter

Click to rate this post!
[Total: 0 Average: 0]

Published in Coding Computers and IT Start

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.