Reading and Writing Files
๐ What is File I/O? File I/O (Input/Output) lets your program read data from files and save results back to disk. Without files, every time you close the program all data is lost. With files, you can load saved games, read configuration, process datasets, and log events.

Appy Saysโฆ
Every real app reads files โ config settings, user data, game saves, CSVs of scores. Python makes file reading beginner-friendly AND safe with the with statement. Learn this and your programs can talk to the filesystem.
What is File Reading in Python?
Python's built-in open() function opens a file and returns a file object. You then call methods on it to read the content. The with keyword automatically closes the file when you're done โ even if an error occurs.
- โข
open('file.txt', 'r')โ open for reading (default mode) - โข
open('file.txt', 'w')โ open for writing (creates or overwrites) - โข
open('file.txt', 'a')โ open for appending - โข
.read()โ returns entire file as one string - โข
.readlines()โ returns a list where each item is one line - โข
.readline()โ reads one line at a time - โขUse
with open(...) as f:โ always use this pattern
Think of it like loading a Minecraft world
When you load a saved Minecraft world, the game opens the save file, reads all the block data, loads it into memory, then closes the file. Python's with open() does exactly this โ open the file, read it, close it automatically, with no data leaks.
How It Works
- โข1.
with open('scores.txt', 'r') as f:opens the file and assigns the file object tof - โข2. Everything indented under
withcan usef - โข3.
content = f.read()reads the whole file into a string - โข4.
lines = f.readlines()reads into a list of strings, each ending in\n - โข5. When the
withblock ends, Python automatically callsf.close() - โข6. Writing: open with
'w'and callf.write('text')
Real-World Examples
- โขA game app reads a high-scores
.txtfile to display the leaderboard - โขSpotify's desktop app reads a local config file for audio settings on startup
- โขA school project reads a CSV of student names to generate personalised certificates
- โขA chatbot reads a
responses.txtfile to load its reply library without hardcoding - โขYouTube's subtitle system reads
.srtcaption files line by line to sync with video
Key Facts
- โขAlways use
with open()โ it's called a context manager and guarantees the file is closed - โขAdd
encoding='utf-8'to handle emoji and non-ASCII characters:open('f.txt', 'r', encoding='utf-8') - โขPython's
csvmodule makes reading CSV files even easier than doing it manually - โขLarge files should use a
for line in f:loop โ this reads one line at a time without loading everything into RAM
Watch Out!
Opening a file with 'w' mode immediately deletes all existing content โ even before you write anything new. If you want to add to an existing file, use 'a' (append) mode instead.
Remember
Always use with open('file.txt') as f: โ never f = open(...) without closing. The with block is the professional standard.
What You Learned
- โข
with open('file.txt', 'r') as f:safely opens and auto-closes files - โขUse
.read()for full content,.readlines()for a line list,'w'to write - โขUnlocks: loading game saves, reading configs, processing data files, building file-based apps
Key Facts
- โAlways use
with open()โ it's called a context manager and guarantees the file is closed - โAdd
encoding='utf-8'to handle emoji and non-ASCII characters:open('f.txt', 'r', encoding='utf-8') - โPython's
csvmodule makes reading CSV files even easier than doing it manually - โLarge files should use a
for line in f:loop โ this reads one line at a time without loading everything into RAM
Real-World Examples
Remember
Always use with open('file.txt') as f: โ never f = open(...) without closing. The with block is the professional standard.
Quick Quiz
with open(...) as f: ensures?