Folder Cleaner in Python!!

Folder Cleaner in Python!!

Introduction to Folder Cleaner

While constantly working on your projects specially on UI/UX designing, you may have experienced a complete mess up of the folder. You may end up in a single unstructured folder which contains all the file types. This may not look good and you may have to manually create folders and then move these files into them. If these problems happens to you then this project is for you. You have to just run this python file in an unstructured folder and that folder will get Structured. We will use python's OS module in order to arrange files. So without wasting more time lets start coding :)

Importing OS module

import os

Defining functions to make directory and move files

def createIfNotExist(foldername):
    '''
    This function will create the directory if it does not exist.
    '''
    if not os.path.exists(foldername):
        os.makedirs(foldername)


def moveFiles(foldername,files):
    '''
    This function will move files to directory.
    '''
    for f in files:
        os.replace(f,f'{foldername}/{f}')

createIfNotExist() is a function which will take folder name as argument and will create that folder if it does not exists. movefiles() function will take two arguments folder name and files and this function will move all the files to the directory.

Defining ArrangeFiles function


def arrangeFiles(current_dir_files,subdirectory_name,file_extensions):
    '''
    This function will arrange the files in proper directories
    '''
    exts = [x.split('.')[1] for x in current_dir_files] # Getting list of all the available file types.
    if any(file in exts for file in file_extensions):   # Checking if specified type of extension is there in folder or not.
        fls = [f for f in current_dir_files if os.path.splitext(f)[1][1:].lower() in file_extensions] # List of all files of specific extensions.
        createIfNotExist(subdirectory_name)
        moveFiles(subdirectory_name,fls)

This will be the most important function in our program. Task of this function is to check which types of files are there in the folder and then separate the file according to its format and then moving them to its own type specified folder.

Here the variable exts will contain the list of all the extensions of the file available in the folder. Then we are checking if any of the file available in the folder matches the specific file format or not. If it matches in any case then we will create a variable fls which will contain all the files of that specific format. Then we will create the folder and move all the files there.

Specifying formats for the file types


# Specify the extensions for different types of files.
img_extensions = ['png', 'jpg', 'jpeg', 'svg']
js_extensions = ['js',]
css_extensions = ['css',]
docs_extensions = ['pdf','docx','xlsx']
py_extensions = ['py']

Here we are specifying extensions for specific file formats. If you want support for more types of files you can add them here. Even you can create more lists of more typs of files.

Defining Main Function


if __name__ == "__main__":
    files = os.listdir()
    if 'main.py' in files:
        files.remove('main.py') # Exculding the program file
    files = [fl for fl in files if os.path.isfile(fl)] # collecting the list of files in current folder.
    #Arranging all the files to specific folders.
    arrangeFiles(files,'img',img_extensions)
    arrangeFiles(files,'docs',docs_extensions)
    arrangeFiles(files,'css',css_extensions)
    arrangeFiles(files,'js',js_extensions)
    arrangeFiles(files,'py',py_extensions)

In main function firstly we will get the list of all the files and folders in the current directory. We will exclude this current running file which is 'main.py' in my project. You must specify the name of the python file in which you are writing this program. Then we are collecting the files and excluding the directories from the files variable. After that we just have to call arrangeFiles() functions for each specified file type and hence our folder cleaner is built.

Conclusion

This is just a beginner level yet useful mini project in python which can boost your confidence to make more higher level projects. To get the source code click here . If you want more projects in python then checkout my Basic Python Projects Github Repo. If you liked it then please share and follow for more such projects ;) Happy Coding :)