Day 8 of 30DaysOfPython: Mastering File Handling in Python

Day 8 of #30DaysOfPython ✅ Until today, every single thing I built vanished the moment I closed the terminal. Run the script, see the output, close it — gone. No trace. No memory. Nothing. File handling changed that completely. I can now write a script that creates a file, puts data inside it, and saves it to my computer — permanently. I can open that same file an hour later and read every line back. My code has storage now. That shift in what Python can do hit harder than I expected. The concept itself isn't complicated. But the details will get you. I spent 20 minutes wondering why my file kept getting wiped every time I ran my script. Turns out I was opening it in "w" mode — which doesn't add to a file. It replaces it. Completely. Every. Single. Time. Switched to "a" for append. Problem solved. Data survived. Lesson learned permanently. 😅 The other habit I picked up today: always use with open() instead of plain open(). It automatically closes the file when you're done — even if your code crashes midway. I didn't think it mattered until I read about what happens to files that never get closed properly. Now I will never skip it. What I covered today: "r" to read, "w" to write, "a" to append with open() as the safe, correct way to handle files .read(), .readlines(), and looping line by line Checking if a file exists before trying to open it Day 8 done. My code has a memory now. 🧠 👇 What was the first script you wrote that actually saved data to a file? I'd love to know what made file handling click for you — drop it below! #Python #30DaysOfPython #FileHandling #BuildInPublic #PythonProjects

To view or add a comment, sign in

Explore content categories