Folder sorter according to files extensions using Python

Published on . Written by

Folder sorter according to files extensions using Python

Many of you might have come across the situation where you want to find a file in a directory and when you open that directory, it is messed up with all kinds of files. It is really difficult to locate the required file. How nice it would be if we can have all the files with the same extension in one directory! The job of finding the file would get simpler and it would look pretty good as well.


Skyfi Labs Projects
This problem is solved by Python. Using Python os and shutil module we can easily sort the files with the same extension and store them in separate folders.

Read more..

SLNOTE

Folder sorter project description

To understand the concept of the project in a better way imagine that a folder is completely messed up with files of all kinds of extensions (.txt, .mp4, .csv, etc). Finding a particular file from this huge ocean of files is very tough.

After running the python script on the above folder, directories with extension names are created and files with the same extension are moved to the corresponding directory.

Python Modules used for projects:

To build this project we have to make use of two Python-modules namely: os-module and shutil-module.

Brief about them is :

OS – Module: os –module provides all the functionalities of interacting with the operating system. It provides a portable way of interacting with os dependent functionalities.  os –module comes under python’s standard utility modules.

Shutil –module: The shutil module is used to manipulate the high-level operations on files and collections of files. In simpler words, shutil module is used to copy, move or remove files. It also comes under the Python standard utility module. The function that we are going to use in our project is shutil.move(src,dest). This function recursively moves a file or directory from source to a given destination. If the destination directory already exists, then the source is moved inside it. But if destination already exists but it is not a directory then it may be overwritten depending upon os.rename() semantics.


SLLATEST
Project Implementation

For the actual implementation of the project, we need to follow the following steps:

  • Create a list of all the filenames in an organized list that are present in the specified directory.
  • In this step iterate through every file and do the following steps according to the conditions.
  • Create a list which contains only extensions (without ‘ . ’ like txt and not ‘ .txt ’ )
  • Now there can be three cases, the first one is if the file itself is a directory. In this case, just continue to iterate.
  • Second case if ‘ ext’ directory already exists. In such a case simply move the source file to the destination.
  • Third case is if ‘ ext ’ directory does not exist, then simply create 'ext’ directory and move all the files with ‘ext’ extension in that directory.
  • Repeat the step 5,6,7, for all files in that directory.
  • After completing the loop, you will have the folders with extension as their name which would include files with the same extensions.
Software requirements: Text Editor (preferably Sublime Text), Python3.

Programming Languages: Python, OS-module, shutil module.


SLDYK
Kit required to develop Folder sorter according to files extensions using Python:
Technologies you will learn by working on Folder sorter according to files extensions using Python:


Any Questions?


Subscribe for more project ideas