r/learnpython • u/Vast_Battle5414 • 44m 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.
•
u/mkhanwrites_ 29m ago edited 26m 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.
•
•
u/baubleglue 4m ago
Not sure if it is a concept, but
if os.path.exist(file_path):
# don't overwrite should do the job
•
u/Correct_Rain_9438 0m 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.
•
•
u/aqua_regis 35m ago
https://automatetheboringstuff.com/3e/chapter11.html