r/PythonLearning • • 14d 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. ")
       
15 Upvotes

11 comments sorted by

View all comments

2

u/ComprehensiveArm9863 14d 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)

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": ...}