Python | Sort and store files with same extension Last Updated : 29 Dec, 2020 Comments Improve Suggest changes Like Article Like Report 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 - 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 - Python3 1== 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_) Output: Comment More infoAdvertise with us Next Article Python | Sort and store files with same extension S SohomPramanick Follow Improve Article Tags : Python Python Programs python-utility python-file-handling Python file-handling-programs Python-sort +2 More Practice Tags : python Similar Reads Fastest Way to Sort in Python Sorting is a fundamental operation in Python. Whether we are working with numbers, strings or complex data structures knowing how to sort efficiently is key to optimizing performance. In this article, we will explore the fastest way to sort in Python breaking down the built-in sorting techniques and 3 min read Get the File Extension from a URL in Python Handling URLs in Python often involves extracting valuable information, such as file extensions, from the URL strings. However, this task requires careful consideration to ensure the safety and accuracy of the extracted data. In this article, we will explore four approaches to safely get the file ex 2 min read How to use OS with Python List Comprehension One such module is the 'os' module, which allows interaction with the operating system. When combined with list comprehensions, the 'os' module becomes a potent tool for handling file and directory operations concisely and expressively. In this article, we will see how we can use the os module with 3 min read How to sort a list of strings in Python In this article, we will explore various methods to sort a list of strings in Python. The simplest approach is by using sort().Using sort() MethodThe sort() method sorts a list in place and modifying the original list directly.Pythona = ["banana", "apple", "cherry"] # Sorting list in place a.sort() 2 min read How to Use Regex with os.listdir() in Python? We are given a file path and our task is to find out the usage of regex with os.listdir() in Python by using that path and files inside that directory. In this article, we will see the usage of Regex with os.listdir() in Python with code examples. Regex with os.listdir() in PythonIn Python, the os.l 3 min read How to Sort a List Alphabetically in Python? Sorting lists alphabetically is a common task in Python, whether we're organizing names, cleaning data or building an interface. Python makes sorting easy with several built-in methods and libraries for both simple and advanced sorting needs.Let's look at some of the most common methods one by one:U 2 min read Sort a list in python Sorting is a fundamental operation in programming, allowing you to arrange data in a specific order. Here is a code snippet to give you an idea about sorting.Python# Initializing a list a = [5, 1, 5, 6] # Sort modifies the given list a.sort() print(a) b = [5, 2, 9, 6] # Sorted does not modify the gi 5 min read Python | Sort Numerical Records in String Sometimes, while working with Python records we can have a problem that they may occur in name and number format in strings. These may be required to be sorted. This problem can occur in many domains in which data is involved. Let us discuss certain ways in which this task can be performed. Method # 6 min read Python - Differential Sort String Numbers and Alphabets Given a List String, Reorder List, with Sorted Alphabets followed by Sorted Strings. Input : test_list = ["1", "G", "10", "L", "9", "K", "4"] Output : ['G', 'K', 'L', '1', '4', '9', '10'] Explanation : Alphabets sorted, succeeded by sorted digits. Input : test_list = ["1", "G", "10", "L", "9"] Outpu 5 min read Rename all file names in your directory using Python Given multiple files in a directory having different names, the task is to rename all those files in sorted order. We can use OS module in order to do this operation. The OS module in Python provides functions for interacting with the operating system and provides a portable way of using operating s 1 min read Like