Day 7 gave our programs a memory! We learned about File I/O (Input/Output), which allows our programs to save data to files and read it back later. This is how applications remember information even after they are closed.
- Cheat Sheet:
day_7_cheatsheet.md
- The
with open()Statement: The standard, safe way to open and automatically close files. - File Modes: Understanding the difference between
'r'(read),'w'(write, which erases), and'a'(append). - Reading Files: Using
.read(),.readlines(), and looping line-by-line. - Writing Files: Using
.write()to save strings to a file. - The
osModule: Usingos.path.exists()to safely check if a file exists before trying to open it.
# A safe way to append a new high score to a file
new_score = "Player 1: 500\n"
# 'a' mode creates the file if it doesn't exist, or adds to the end if it does.
with open('scores.txt', 'a') as file:
file.write(new_score)
print("Score saved successfully!")