r/learnpython • • 11h 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

13 comments sorted by

8

u/aqua_regis 11h ago • Bravo

0

u/Unique-Confusion-982 9h ago

the practice project at the bottom of that page is literally a selective file copy script

2

u/aqua_regis 8h ago

Guess why I posted the link...

5

u/szank 11h 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

4

u/bluejacket42 11h ago

Read docs

3

u/sparkas 11h 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

3

u/mkhanwrites_ 11h ago edited 11h 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 11h 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/RngdZed 10h ago

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

1

u/Few_Care_1657 10h 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?

0

u/baubleglue 11h ago

Not sure if it is a concept, but

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

should do the job