Skip to main content

Command Palette

Search for a command to run...

Automate boring files organizing using python (Python files organizer)

Updated
•3 min read•View as Markdown
Automate boring files organizing using python (Python files organizer)

Intro

Hello today we are going to code a files organizer using python script it will organize the files by their types for example it will put all files with extension (.png) in a folder called Pictures let's code ... (this post is published here Python files organizer, automate boring files organizing using python)

Coding

Importing needed modules

those are the modules we are going to use

if you have any problem you can post it on comments

import os
import pathlib
import shutil
import fnmatch

Preparing the default folder for every type of files

So now we are going to set a folder for every type for example we want to move the pictures to the folder in path '/home/rin/Downloads/'

in my case i am using linux on windows the paths will look like ' C:\rin\Brink\Pictures'

## define path for every category
file_folder={
    'Music':'/home/rin/Downloads/',
    'Pictures':'/home/rin/Pictures/',
    'Documents':'/home/rin/Documents/',
    'Archives':'/home/rin/Archives/',
    'Videos':'/home/rin/Videos/',
    'Codes':'/home/rin/Codes/'
}

Defining the type of file by the extension

What's extension ?

A file extension or file name extension is the ending of a file that helps identify the type of file in operating systems, such as Microsoft Windows. In Microsoft Windows, the file name extension is a period that is often followed by three characters but may also be one, two, or four characters long.

As an example, the file name "myfile.txt" has a file extension of ".txt," which is a file name extension associated with text files.

So code :

## define the type of every extension
file_type={'Pictures':['.jpg', '.jpeg', '.jpe','.png','.jp2','.ico', '.wbmp','.j2k', '.jpf', '.jpx', '.jpm', '.mj2','.svg', '.svgz','.webp','.gif']
,'Music':['mp3']
,'Videos':['.mp4','.srt','.mkv','.3gp','.m4a']
,'Documents':['.pdf','.docx','.doc','.csv','.txt','.xls','.xlsx','.log']
,'Archives':['.zip','.tar']
,'Codes':['.py']}

Main code

Now we take the path of the folder contains the files we want organize as example for me i want clean my download folder

path='/home/rin/Downloads/'

So now what we want to do is extracting all the files in the folder and check the type of every file then move it to the folder contains its type

files=os.listdir(path)# list of files in the folder
for file in files:#for for every file in the list
    extension=pathlib.Path(file).suffix #extension of file ex:(.txt)
    #Documents
    if extension in file_type['Documents']:#check if the file is a document
        move=pathlib.Path(path+file).rename(file_folder['Documents']+file)# move the file to the folder conatins documents
        #move=shutil.move(path+file,file_folder['Documents'])
        print('success #### {} to {}'.format(file,move))
    #Music    
    if extension in file_type['Music']:
        move=pathlib.Path(path+file).rename(file_folder['Music']+file)
        #move=shutil.move(path+file,file_folder['Music'])
        print('success #### {} to {}'.format(file,move))
    #Pictures
    if extension in file_type['Pictures']:
        move=pathlib.Path(path+file).rename(file_folder['Pictures']+file)
        #move=shutil.move(path+file,file_folder['Pictures'])
        print('success #### {} to {}'.format(file,move))
    #Videos
    if extension in file_type['Videos']:
        move=pathlib.Path(path+file).rename(file_folder['Videos']+file)
        #move=shutil.move(path+file,file_folder['Videos'])
        print('success #### {} to {}'.format(file,move))
    #Archives
    if extension in file_type['Archives']:
        move=pathlib.Path(path+file).rename(file_folder['Archives']+file)
        #move=shutil.move(path+file,file_folder['Archives'])
        print('success #### {} to {}'.format(file,move))
    #Codes
    if extension in file_type['Codes']:
        move=pathlib.Path(path+file).rename(file_folder['Codes']+file)
        #move=shutil.move(path+file,file_folder['Codes'])
        print('success #### {} to {}'.format(file,move))
    else:
        print('## '+file)

Note : You can add more types

Full source code

DON'T FORGET TO CHANGE THE PATHS ALSO YOU CAN ADD MORE CATEGORY BE SMART ..

import os
import pathlib
import shutil
import fnmatch
## define path for every category
file_folder={
    'Music':'/home/rin/Downloads/',
    'Pictures':'/home/rin/Pictures/',
    'Documents':'/home/rin/Documents/',
    'Archives':'/home/rin/Archives/',
    'Videos':'/home/rin/Videos/',
    'Codes':'/home/rin/Codes/'
}
## define the type of every extension
file_type={'Pictures':['.jpg', '.jpeg', '.jpe','.png','.jp2','.ico', '.wbmp','.j2k', '.jpf', '.jpx', '.jpm', '.mj2','.svg', '.svgz','.webp','.gif']
,'Music':['mp3']
,'Videos':['.mp4','.srt','.mkv','.3gp','.m4a']
,'Documents':['.pdf','.docx','.doc','.csv','.txt','.xls','.xlsx','.log']
,'Archives':['.zip','.tar']
,'Codes':['.py']}

path='/home/rin/Downloads/'
files=os.listdir(path)# list of files in the folder
for file in files:#for for every file in the list
    extension=pathlib.Path(file).suffix #extension of file ex:(.txt)
    #Documents
    if extension in file_type['Documents']:#check if the file is a document
        move=pathlib.Path(path+file).rename(file_folder['Documents']+file)# move the file to the folder conatins documents
        #move=shutil.move(path+file,file_folder['Documents'])
        print('success #### {} to {}'.format(file,move))
    #Music    
    if extension in file_type['Music']:
        move=pathlib.Path(path+file).rename(file_folder['Music']+file)
        #move=shutil.move(path+file,file_folder['Music'])
        print('success #### {} to {}'.format(file,move))
    #Pictures
    if extension in file_type['Pictures']:
        move=pathlib.Path(path+file).rename(file_folder['Pictures']+file)
        #move=shutil.move(path+file,file_folder['Pictures'])
        print('success #### {} to {}'.format(file,move))
    #Videos
    if extension in file_type['Videos']:
        move=pathlib.Path(path+file).rename(file_folder['Videos']+file)
        #move=shutil.move(path+file,file_folder['Videos'])
        print('success #### {} to {}'.format(file,move))
    #Archives
    if extension in file_type['Archives']:
        move=pathlib.Path(path+file).rename(file_folder['Archives']+file)
        #move=shutil.move(path+file,file_folder['Archives'])
        print('success #### {} to {}'.format(file,move))
    #Codes
    if extension in file_type['Codes']:
        move=pathlib.Path(path+file).rename(file_folder['Codes']+file)
        #move=shutil.move(path+file,file_folder['Codes'])
        print('success #### {} to {}'.format(file,move))
    else:
        print('## '+file)

Feel free to develop the code and post any problem or request in the comments

J

Hi! Good work.

I have some feedback which might help you refactor this code to make it more flexible and less open to bugs appearing

  1. There is a lot of repeated/duplicate code in your if statement. Have you thought about creating a method which performs the move file code?
  2. The code above relies on the directories in file_folders dictionary existing. Have you thought about what would happen if those directories hadn't been created prior to the program running?
  3. The code above has the file_folder 'types' (documents, pictures, etc) hard coded in your if statement. Have you thought about creating a method that gets the key from the file extension?
  4. Do you need both the file_folder and file_type dictionaries, or can you turn them into a single dictionary?

I personally would turn this into a Python class too, so that I can inject the file types, file folders and the path I'm moving files from into the class.

Happy coding!

1
B

thank you so much for your feedback. i agree with you about turning this into a python class it will make the code more clean and remove the repeated/duplicate code. and specially the methods. thank you again for your comment