Python | Arrange the files in directories according to extensions Last Updated : 09 Sep, 2019 Improve Improve Like Article Like Save Share Report In this article, we will learn how to arrange the files in different directories according to their extensions. When you have a lot of files with different extension than you can simply use the script for your help. Below is the Python implementation – import os # Using system functions import shutil # Moving file to different folders. # Changing the folder location. os.chdir(os.getcwd()) cwd = os.getcwd() # For listing files from the folder. l = [f for f in os.listdir(cwd) if os.path.isfile(f)] l2 = [] # Get the extension of file from list l. for value in l: s = value.split('.')[1] l2.append(s) print(l, l2) # We remove duplicate values from # list l2 and checks if directory # exists otherwise we create new one for extension in set(l2): dirname = extension if os.path.exists(cwd+'\'+extension): pass else: os.makedirs(dirname) # We use zip function and list l and # l2 passed as arguments. # If extension in file is same and # file not exists then we move the file. for files, extension in zip(l, l2): if extension in files: if os.path.exists(cwd+'\'+extension+'\'+files): pass else: shutil.move(cwd+'\'+files, cwd+'\'+extension) print(extension, files) else : print('error') Files Not Arranged: Files Arranged: Output: ['arrange.py', 'mydata.pdf', 'python.exe', 'resume.doc'] ['py', 'pdf', 'exe', 'doc'] py arrange.py pdf mydata.pdf exe python.exe doc resume.doc Like Article Suggest improvement Previous Python | shutil.copyfile() method Next Python | Convert image to text and then to speech Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads Python | Move or Copy Files and Directories How to move Files and Directories in Python How to List all Files and Directories in FTP Server using Python? Python - Move Files To Creation and Modification Date Named Directories Listing out directories and files in Python Create temporary files and directories using tempfile Python - Loop through files of certain extensions Python: Get List of all empty Directories Make multiple directories based on a List using Python How to merge multiple excel files into a single files with Python ? Like vedraj360 Follow Article Tags : python-utility Python Python Programs Practice Tags : python