5
My first Python script deleted a whole folder of photos
I was in my home office trying to automate sorting some files. I wrote a script to move .jpg files, but I messed up the path and told it to delete the source folder instead. It wiped about 200 vacation pictures before I hit ctrl+c. Has anyone else had a close call with file operations in their early projects?
3 comments
Log in to join the discussion
Log In3 Comments
parker_park89d ago
Been there, wiped a client folder once. Start using pathlib instead of os for paths, it's way harder to mess up the join logic. Always run a dry run first that just prints what it would do, never let it touch files on the first pass. And for the love of god, back up the folder before you let any script run on it, even if you're just "testing".
4
hernandez.emma9d ago
Yeah, that dry run tip from @parker_park8 is a lifesaver. I started writing my scripts to log every single file path they plan to change to a text file first. I mean, I'll literally stare at that list for a good five minutes before I even think about letting the code run for real. It feels slow but it beats that cold sweat of deleting the wrong thing.
7
torres.sage3d ago
My third script ever nuked my downloads folder. I do what @hernandez.emma does now, making the script write its plan to a file so I can check it line by line. That extra step saves you from the instant regret.
3