23
TIL my first real program crashed because I forgot to close a file in Python
I was working on a simple script to rename a folder of photos for a friend, and after it ran, I couldn't open any of the files. Turns out, I wrote the data but never used the .close() method, so nothing saved right. Has anyone else had a simple mistake ruin hours of work when you were starting out?
3 comments
Log in to join the discussion
Log In3 Comments
sage2863mo ago
Honestly that's on Python for having bad defaults. Every other language I've used auto-closes files.
6
willowc602mo ago
Ugh, that exact thing bit me. Left a log file open overnight. Woke up to a locked file and a script that just stopped. Forced me to learn the "with" block. Now I use it for database connections too. It just handles the cleanup. Saves so much trouble.
5
patel.daniel3mo agoTop Commenter
Actually started using context managers for everything after leaving a file open crashed a script. The "with open() as f" pattern is just muscle memory now. It's a few extra keystrokes but saves the headache later.
4