Introduction
Python offers an efficient solution to organize files as in today’s digital world, the files quickly pile up in our computers, ranging from images to documents, audio, and video files. Manually organizing these files into folders is time-consuming and repetitive, especially when we can leverage the power of the OS module of Python to handle it automatically. In this guide, we’ll break down a Python script that organizes files based on their type, creating folders for images, documents, audio, video, and other files using OS and Shutil modules of Python.
Let us start understanding the code step by step
Step1: Importing Python modules
import os
import shutil
PythonOS module to interact with the operating system and handle folders.
shutil to perform copy and moving type operations on the folders.
Step 2: Setting Up the Directory
directory=r'D:\os' # complete path of the folder where you have to organize your files
PythonStore the path of the folder where you have to organize the files as a string.
Step 3: Defining the Function
def file_organize(directory):
PythonDefining a function file_organize with directory as a parameter.
Step 4: Checking Directory Validity
if not os.path.isdir(directory):
print('Path incorrect. Directory does not exist')
return
PythonBefore we begin moving files, we check if the given directory exists using os.path.isdir()
. If the directory is incorrect, a message is displayed, and the function exits. This validation step is essential to avoid runtime errors and to ensure the path we provided is accurate.
Step 5: Iterating Over Files
for file_name in os.listdir(directory):
file_path=os.path.join(directory,file_name)
PythonUsing os.listdir()
, we get a list of all files and folders within the specified directory. Then, for each item, we build its full path by combining the directory path with the file name using os.path.join()
.
For more such content and regular updates, follow us on Facebook, Instagram, LinkedIn
Step 6: Skipping Subdirectories
if os.path.isdir(file_path):
continue
PythonIn this script, our goal is to organize files only. To avoid inadvertently moving folders, we check if the current item is a directory using os.path.isdir()
. If it is, we skip it with continue
.
Step 7: Extracting File Extension
file_extension=os.path.splitext(file_name)[1].lower()
PythonUsing os.path.splitext()
, we split the filename and get the file extension, which helps identify the file type. Converting the extension to lowercase ensures our script remains case-insensitive. At ConsoleFlare, we cover such techniques to make code robust and efficient, crucial skills in data science.
Step 8: Classifying Files by Type
if file_extension in ['.jpg','.jpeg','.png','.gif']:
folder_name='Images'
elif file_extension in ['.pdf','.docx','.txt'] :
folder_name='Documents'
elif file_extension in ['.xlsx','.csv']:
folder_name='Excel'
elif file_extension in ['.mp3','wav']:
folder_name='Audio'
elif file_extension in ['.mp4', 'mkv','.mov']:
folder_name='Video'
else:
folder_name='Others'
PythonHere, we use if
and elif
statements to group files based on their extensions. Each group corresponds to a specific category, such as Images, Documents, Excel, Audio, Video, or Others. Understanding this logic is vital for beginners as it demonstrates how Python can simplify tasks through automation.
Step 9: Creating Category Folders
category_folder = os.path.join(directory,folder_name)
os.makedirs(category_folder,exist_ok=True)
PythonOnce we know the category, we create the corresponding folder in our target directory using os.makedirs()
. Setting exist_ok=True
ensures that the folder is created only if it doesn’t already exist, preventing errors.
Step 10: Moving Files and Calling the Function
shutil.move(file_path,os.path.join(category_folder,file_name))
print(f'Moved:{file_name} to {folder_name}')
file_organize(directory)
PythonFinally, we use shutil.move()
to transfer files from the main directory to the respective category folders. Each successful move is accompanied by a print statement confirming the transfer.
This initiates the organization process for the specified directory, moving files into their designated folders.
Real-World Applications: Why This Matters in Data Science
Automation is a vital aspect of data science and is widely used in tasks such as data processing, web scraping, and file management. This script showcases foundational automation skills that are highly transferable to data science roles. For example, large datasets can be split, sorted, or cleaned based on criteria, streamlining data preparation.
Read more about DATA SCIENCE
If you’re ready to embark on a rewarding career in data science, consider enrolling in a comprehensive course that focuses on Python.
At ConsoleFlare, we offer tailored courses that provide hands-on experience and in-depth knowledge to help you master Python and excel in your data science journey. Join us and take the first step towards becoming a data science expert with Python at your fingertips.
Register yourself with ConsoleFlare for our free workshop on data science. In this workshop, you will get to know each tool and technology of data analysis from scratch that will make you skillfully eligible for any data science profile.
Thinking, Why Console Flare?
- Recently, ConsoleFlare has been recognized as one of the Top 10 Most Promising Data Science Training Institutes of 2023.
- Console Flare offers the opportunity to learn Data Science in Hindi, just like how you speak daily.
- Console Flare believes in the idea of “What to learn and what not to learn” and this can be seen in their curriculum structure. They have designed their program based on what you need to learn for data science and nothing else.
- Want more reasons,
Register yourself & we will help you switch your career to Data Science in just 6 months.
The Complete Code:
import os
import shutil
directory=r'D:\os'
def file_organize(directory):
if not os.path.isdir(directory):
print('Path incorrect. Directory does not exist')
return
for file_name in os.listdir(directory):
file_path=os.path.join(directory,file_name)
if os.path.isdir(file_path):
continue
file_extension=os.path.splitext(file_name)[1].lower()
if file_extension in ['jpg','.jpeg','.png','.gif']:
folder_name='Images'
elif file_extension in ['.pdf','.docx','.txt'] :
folder_name='Documents'
elif file_extension in ['.xlsx','.csv']:
folder_name='Excel'
elif file_extension in ['.mp3','wav']:
folder_name='Audio'
elif file_extension in ['.mp4', 'mkv','.mov']:
folder_name='Video'
else:
folder_name='Others'
category_folder = os.path.join(directory,folder_name)
os.makedirs(category_folder,exist_ok=True)
shutil.move(file_path,os.path.join(category_folder,file_name))
print(f'Moved:{file_name} to {folder_name}')
file_organize(directory)
Python