r/learnpython • • 3d 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

14 comments sorted by

8

u/aqua_regis 3d ago • Bravo

0

u/Unique-Confusion-982 3d ago

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

2

u/aqua_regis 3d ago

Guess why I posted the link...

5

u/szank 3d 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 3d ago

Read docs

3

u/sparkas 3d 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_ 3d ago edited 3d 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 3d 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 3d ago

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

1

u/Capital-Vast-5951 2d ago

I’d start with pathlib and get the script to print a plan before it changes anything: one source → destination line per file, with a clear SKIP when the destination already exists. Test it on a temporary folder, and put a file with the same name in the destination folder to make sure it gets skipped. Skipping collisions is a good first version; you can add suffixes like (1) later. I’d only add shutil.move after the printed plan looks right.

1

u/Few_Care_1657 3d 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 3d ago

Not sure if it is a concept, but

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

should do the job