RisingCoder Full course โ†’
Day 1Day 2Day 3Day 4
๐Ÿ“‚
STUDENT NOTES ยท DAY 13 OF 24

The Memory Chest ๐Ÿ“‚

Close your program and every variable vanishes. Files are the treasure chest where data survives.

1Writing your first file

๐Ÿค–
Bittu: Variables live in my short-term memory โ€” poof at shutdown. Files go on the DISK. They survive restarts, power cuts, everything.
๐Ÿฆ‰
Professor Hoot: The with block closes the file automatically when done. Always use it โ€” no forgotten open files.

2Reading it back

3The three modes

๐Ÿ“ "w" write โ€” wipes the file and starts fresh!   โž• "a" append โ€” adds at the end   ๐Ÿ‘€ "r" read โ€” just looks

๐Ÿž
Bugly: Heheheโ€ฆ open an important file with "w" by mistake and everything inside is GONE. My masterpiece.

4Reading a file that isn't there

๐Ÿ
Py: FileNotFoundError โ€” read mode needs the file to exist. Tomorrow we learn to catch errors like this gracefully!

5Quick quiz! ๐Ÿง 

You open an existing diary with open("diary.txt", "w"). What happens to the old entries?
๐Ÿ’ก "w" erases and starts over. To add to the end, use "a" for append.
Why use with open(...) as f:?
๐Ÿ’ก The with-block guarantees cleanup โ€” the file closes even if something goes wrong inside.

6Your mission ๐Ÿš€

Mission 1: Write your 3 favourite movies to movies.txt, one per line.
Mission 2: Read the file back and print each line with a loop.
Mission 3: Append a 4th movie with "a" mode, re-read, and confirm all 4 are there.

Day 13 done โ€” your programs have memory now! ๐Ÿ’พ

Next: exception handling โ€” programs that survive mistakes instead of crashing.

Go to Day 14: Error Shields โ†’

๐ŸŽฎ Or test yourself in the Python Arcade โ†’