Python | Sort and store files with same extension

Have you ever wanted to find any particular file in a folder, but then completely freak out when you find that folder to be a hell of a mess? Well, Python is a rescue here.
Using Python OS-module and shutil module, we can organize the files with same extensions and store in separate folders.

Look at the image shown below –
Unorganized Folder

This folder is fully unorganized. 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.



Below is the Python implementation –

filter_none

edit
close

play_arrow

link
brightness_4
code

import os
import shutil
   
# Write the name of the directory here,
# that needs to get sorted
path = '/path/to/directory'
  
  
# This will create a properly organized 
# list with all the filename that is
# there in the directory
list_ = os.listdir(path)
   
# This will go through each and every file
for file_ in list_:
    name, ext = os.path.splitext(file_)
  
    # This is going to store the extension type
    ext = ext[1:]
  
    # This forces the next iteration,
    # if it is the directory
    if ext == '':
        continue
  
    # This will move the file to the directory
    # where the name 'ext' already exists
    if os.path.exists(path+'/'+ext):
       shutil.move(path+'/'+file_, path+'/'+ext+'/'+file_)
  
    # This will create a new directory,
    # if the directory does not already exist
    else:
        os.makedirs(path+'/'+ext)
        shutil.move(path+'/'+file_, path+'/'+ext+'/'+file_)

chevron_right


Output:
Organized Folder



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.