📢
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?
2 comments

Log in to join the discussion

Log In
2 Comments
sage286
sage28616d ago
Honestly that's on Python for having bad defaults. Every other language I've used auto-closes files.
6
patel.daniel
patel.daniel16d 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