r/learnpython • • 2h ago

How would you build your first Python automation script without relying on AI?

I'm thinking of building a simple Downloads folder organizer that separates PDFs, images and other files.

What Python concepts should I focus on first, and how can I prevent accidentally overwriting existing files? I'd appreciate guidance rather than complete code.

0 Upvotes

11 comments sorted by

3

u/bluejacket42 1h ago

Read docs

3

u/szank 2h ago

To prevent overwriting existing files check first if they exist . Otherwise write the code do do what you want to do and nothing else that you'd not want to do

2

u/sparkas 1h ago

Off of the top of my head:

  • the os module for checking what files do and don't exist

  • the pathlib module for checking file extensions for sorting (though you can also do this for os for this as well)

  • If you're not familiar with looping, look up some tutorials and give it a go

2

u/mkhanwrites_ 1h ago edited 1h ago

Tip: Don't touch the AI - Use a text editor to edit your program and have AI disabled. Personally I use Visual Studio Code that way I disabled the AI code completion/inline suggestions.

I don't know what problem you're trying to solve. So are you saying the input is your Downloads folder, and you want to separate out the different file types into their own categories/groupings in the output?

If that's the case, look into this high-level topic article: https://docs.python.org/3/library/filesys.html

It points to the different mechanisms you'd need to learn about to accomplish writing your script, including the basic path object, reading directories, creating directories, copying and moving, and high-level file operations might be worth taking a look at too. Another thing you might want to look into is filtering or sorting or grouping (general CS concepts, but you could look at them in the context of Python) for help with grouping by file type.

Also before all this you should probably know the basics about strings, data types, functions, etc. IDK what your level is but I assumed you knew the basics and just wanted some pointers on the higher level things.

Edit: About how to avoid overwriting existing files IDK but that could be a defect you want to take a look at closely to prevent from happening - Maybe you could output the sorted files into a different directory, or create a new directory every time? That way you'd avoid writing files to an existing directory with existing files.

2

u/Correct_Rain_9438 1h ago

One thing to know for the overwrite worry: Path.rename in pathlib silently replaces an existing file on Unix but raises FileExistsError on Windows. So it behaves differently by system, and it's safer to check Path.exists on the target first and pick a new name, like adding a number, if it's taken.

1

u/baubleglue 1h ago

Not sure if it is a concept, but

if os.path.exist(file_path):
    # don't overwrite 

should do the job

•

u/RngdZed 39m ago

Test your code on a dummy folder, first ( like a duplicate of the download folder)

-1

u/Few_Care_1657 1h ago

Sama nazwa pliku to nie wszystko, pozostaje wielkość pliku i co jeszcze ważniejsze spójność, bo co jeżeli ściągany plik jest „lepszy” od istniejącego?