Automatic File Sorter With Python

by Gaganram PM in Circuits > Software

5061 Views, 11 Favorites, 0 Comments

Automatic File Sorter With Python

before.JPG
Untitled-1.jpg
download (1).jpg

File management is something that we deal with almost everyday. Imagine one day you want to sort the files to folder according to its extension. This will be a very difficult and time consuming task. Look at your downloads directory it is filled with different files and you sometimes need to search for a specific file, this will take you a long time. But if it was made easy. With one click of mouse you a sort them in no time. How is this even possible??

We can create program in python which can make us to spend time on other things than searching files. This program will help us in sorting files according to the file extension, then it will be easy task for us to find the file by looking at the extension. Imagine if you want a music file, all the music file will be under mp3 folder, this will make us easy to find.


Python is a very good programming language that has everything in it. I personally like python because you can almost do everything in it. Python has many modules that help to make more programs. Python can also be used in automating our day to day tasks.

If you are told to find a particular file in this folder (or maybe an even larger folder with thousands of files), you will be stuck and become completely dumbstruck. It might be very tough (even impossible) to find a file from this ocean of mess. This problem can be solved using Python with a few lines of code. Let’s see how can we do this.

You should have some basic knowledge of python to continue.

So lets make it!

Supplies

Python installed on the PC: Python download

Any code editor of your wish

(I personally like VS Code, because it is easy and has many features in it. It is completely free of cost)

Install the Modules

To automate the file sorting you need only two modules:

OS module

The OS module in Python provides functions for interacting with the operating system. OS comes under Python's standard utility modules. The *os* and *os. path* modules include many functions to interact with the file system.

To install the os module:

Go to VS code and open the terminal and type the following:

(you can also use command prompt or powershell)

pip install os-sys

Shutil module

The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.

To install the shutil module:

type the following code:

pip install pytest-shutil

Lets Start Codding!!

1. Import the modules

#importing the os module 
import os  
#importing the shutil module 
import shutil 

2. Define the path to directory

#Write the name of the directory here eg downloads path = '/path/to/directory'

3. Create a list of all the filename

list_ = os.listdir(path)

4. Add for loop to check all the files

for file_ in list_: <br>    name, ext = os.path.splitext(file_)

5. Store the extensions

ext = ext[1:]

5. To repeat add this

if ext == '':
continue

6. Move the files according to the extension

if os.path.exists(path+'/'+ext): <br>       shutil.move(path+'/'+file_, path+'/'+ext+'/'+file_)

7. Create a new folder if there is new type of extension

else: <br>        os.makedirs(path+'/'+ext) 
        shutil.move(path+'/'+file_, path+'/'+ext+'/'+file_)

The Final Code

code.JPG
import os <br>import shutil 

path = '/path/to/directory'

list_ = os.listdir(path)

for file_ in list_: 
	name, ext = os.path.splitext(file_)

	ext = ext[1:]

	if ext == '': 
		continue

	if os.path.exists(path+'/'+ext): 
		shutil.move(path+'/'+file_, path+'/'+ext+'/'+file_)
	else: 
		os.makedirs(path+'/'+ext) 
		shutil.move(path+'/'+file_, path+'/'+ext+'/'+file_)

Run the Code and Done

after.JPG

Press the run button and go to that folder you will have nice and clean organized folder.

-

-

If you have any queries please type them in discussions.

-

-

Thank you :)

Note : I will add the video few days later. Please be tuned...