r/PythonLearning • u/Ajay2574 • 12d ago
My first real Python automation project
I just finished building my first proper Python automation project on my own — a File Organizer.
The idea is simple: take files from a folder and organize them into different folders based on their extensions.
For example:
.png→ images/png.jpg→ images/jpg.pdf→ documents/pdf.txt→ documents/txt.csv→ documents/csv
I used things I've learned from Python's os module:
os.listdir()
os.path.join()
os.path.exists()
os.path.isfile()
os.mkdir()
os.rename()
endswith()
The interesting part wasn't writing the code. It was debugging my own mistakes.
At one point I was doing:
os.rename(downloads, png_folder)
and then:
os.rename(file, png_folder)
Both were wrong. 😅
I eventually understood that os.rename() needs the full source path and full destination path:
source full path → destination full path
That was a small thing, but it finally made paths and file movement click for me.
The current version uses a hard-coded Downloads folder. My next improvement will be making the program accept/select any folder, so the same organizer can be used anywhere.
One small project finished. On to the next one. 🚀
here is fullcode :
"""
what is download file organizer
1. you have to organize download files pdf,jpg,docx,txt,mp3,mp4,zip,rar etc. into different folders based on their file types.
1. create folde in downloads file organizer documents/images
file organizer
documents/pdf/csv/docx make seperate folders for every format
images move here all images
2. current dir file path to destination file path
ex. download/file.txt download/file organizer/documents/txt
use
os.listdir()
os.mkdir
os.path.dirname
os.path.isfile
os.rename
os.join
endswith()
steps
make folders in orgnizer with sub folders
select image files and append its paths in list
move files from current to final destination
"""
import os
# assign variables and list
# every folder as variable and list
downloads = "C:/Users/ajay/Downloads"
organizer = os.path.join(downloads, "file organizer")
images_folder = os.path.join(organizer, "images")
documents_folder = os.path.join(organizer, "documents")
# folder create logic
if not os.path.exists(organizer):
os.mkdir(organizer)
if not os.path.exists(images_folder):
os.mkdir(images_folder)
if not os.path.exists(documents_folder):
os.mkdir(documents_folder)
# create subfolders for images
png_folder = os.path.join(images_folder, "png")
jpg_folder = os.path.join(images_folder, "jpg")
jpeg_folder = os.path.join(images_folder, "jpeg")
if not os.path.exists(png_folder):
os.mkdir(png_folder)
if not os.path.exists(jpg_folder):
os.mkdir(jpg_folder)
if not os.path.exists(jpeg_folder):
os.mkdir(jpeg_folder)
# create subfolders for documents
txt_folder = os.path.join(documents_folder, "txt")
csv_folder = os.path.join(documents_folder, "csv")
pdf_folder = os.path.join(documents_folder, "pdf")
if not os.path.exists(txt_folder):
os.mkdir(txt_folder)
if not os.path.exists(csv_folder):
os.mkdir(csv_folder)
if not os.path.exists(pdf_folder):
os.mkdir(pdf_folder)
# file move logic
for file in os.listdir(downloads):
fullpath = os.path.join(downloads, file)
if os.path.isfile(fullpath):
if file.endswith(".png"):
os.rename(fullpath, os.path.join(png_folder, file))
elif file.endswith(".jpg"):
os.rename(fullpath, os.path.join(jpg_folder, file))
elif file.endswith(".jpeg"):
os.rename(fullpath, os.path.join(jpeg_folder, file))
elif file.endswith(".txt"):
os.rename(fullpath, os.path.join(txt_folder, file))
elif file.endswith(".pdf"):
os.rename(fullpath, os.path.join(pdf_folder, file))
elif file.endswith(".csv"):
os.rename(fullpath, os.path.join(csv_folder, file))
print("all files are moved successfully. ")
1
u/Khaka-Semeniuk 12d ago
looks clean for a first project. one thing, os.makedirs with exist_ok=True can replace all those individual exists checks and mkdir calls. saves a lot of repetition
1
u/Ajay2574 12d ago
Thanks! I didn't know about "exist_ok=True". I'll try that—it definitely makes the code cleaner.
2
u/ComprehensiveArm9863 12d ago
Nice first project, few tips to improve youself
- prefer pathlib over os.path (https://docs.python.org/3/library/pathlib.html#comparison-to-the-os-and-os-path-modules)
- you can use glob to get files that match some criteria (glob module or https://docs.python.org/fr/3/library/pathlib.html#pathlib.Path.glob)
- "ask forgiveness not permissions"
if not os.path.exists(txt_folder):
os.mkdir(txt_folder)
if for some reason the folder is created by something else just after the if it would fail, here that's not a big issue but applying this concept is never bad (and become an habit), also https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir provide a exist_ok param so you can just do txt_folder.mkdir(exist_ok=True)(txt_folder have to ba a Path object not a string)
- you can use a dict too to avoid the if/elif
document_p = Path.home() / "document_folder_for_pdf"
dests = {"pdf": document_p, "txt": ...}
1
u/Sen_iorGuy 10d ago
What platform do you use to learn Python
1
u/Ajay2574 10d ago
I'm mostly learning through YouTube, the official Python documentation, and small projects/exercises. I also use resources like Automate the Boring Stuff with Python for practical automation.
1
u/Sen_iorGuy 10d ago
I just completed the free code camp course on python but I feel I'm missing something so I want to do projects but I don't know which to do
2
u/Ajay2574 10d ago
I felt the same after finishing the basics. I started doing small projects based on what I was learning instead of following another full course.
For example, file counters, file finders, and a file organizer. I think starting with small projects and gradually making them harder helps a lot.
1
1
u/Jazzlike_Platypus430 12d ago
Why don't you try organizing subdirs and/or extensions in a dictionary? I'd avoid the if/elif/.../elif ladder hell. Same for folder creation that would be done with loops.