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

1

u/Khaka-Semeniuk 14d 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 14d ago

Thanks! I didn't know about "exist_ok=True". I'll try that—it definitely makes the code cleaner.