![](https://jrdyishere.com/wp-content/uploads/2024/03/python-2.png)
This is a mini project that I’ve decided to help make my everyday life more organised. Looking at my files it’s usually in a big pile of mess with different formats of files mixed together. Ultimately I wanted to fix that, I’ve created a file organiser by importing os and shutil which allows me to use high levels of operation for file organisation. I then created a function called “file_organiser” which sorts the files inside the directory into separate folders based on their types. The bottom variable allows the user to insert the file directory. Lastly we call the function above to run using the directory variable we just inserted. Then, we can just run the program and you have yourself an organised folder 😀
import os
import shutil
def file_organiser(directory):
# Creating a variable that gets the list of files inside the directory
files = os.listdir(directory)
# Using a for loop to allow iterate over each file type
for file in files:
file_path = os.path.join(directory, file)
# Checking if the file is a regular file
if os.path.isfile(file_path):
file_extension = os.path.splitext(file_path)[1]
# Creating a new file folder if it doesn't exist for the file type
if not os.path.exists(os.path.join(directory, file_extension)):
os.makedirs(os.path.join(directory, file_extension))
# Moving the files to a corresponding folder/directory
shutil.move(file_path, os.path.join(directory, file_extension, file))
# Insert your file directory here
insert_file_directory = 'Example:C:\Downloads'
file_organiser(insert_file_directory)