π Quick Recap (Day 14)
You protected class data with encapsulation and properties.
You created and applied a custom decorator to log method calls.
π― What Youβll Learn Today
Why reading from and writing to files is essential.
How to open, write to, and close files safely.
Using context managers (
withstatement) for automatic cleanup.Handling errors gracefully with
try,except, andfinally.
π Why File I/O Matters
Computers need a way to persist information across runsβlike saving high scores, logs, or user data. File I/O lets your program:
Save results so you can re-open them later.
Share data between different parts of your code or programs.
Log events and errors for debugging and auditing.
Without file I/O, your programβs data would vanish when it stops running.
π Opening and Writing Files
To write data:
Open a file in write mode (
'w'):file = open('data.txt', 'w')Write text using
write()orwritelines():file.write('Hello, world!\n') file.writelines(['Line 2\n', 'Line 3\n'])Close the file to flush data to disk:
file.close()
If you open in 'w' mode, the file is created or truncated (emptied) if it exists.
π Reading Files Safely
To read data back:
Open in read mode (
'r'):file = open('data.txt', 'r')Read entire content or line by line:
content = file.read() # all text lines = file.readlines() # list of linesClose afterward:
file.close()
Reading line by line in a loop:
with open('data.txt', 'r') as f:
for line in f:
print(line.strip())This prints each line without extra blank lines.
π Using Context Managers
The with statement automates closing:
with open('data.txt', 'w') as f:
f.write('Hello, with!\n')
# f is closed automatically hereContext managers prevent resource leaks and errors from forgetting close().
π Handling Exceptions
File operations can fail (missing file, no permissions) sometimes. Use try/except:
try:
with open('missing.txt', 'r') as f:
data = f.read()
except FileNotFoundError:
print('Error: missing file.')
except IOError:
print('Error: cannot read file.')
finally:
print('Finished file attempt.')exceptcatches specific errors and prevents crashes.finallyalways runs (cleanup or final message).
π§ββοΈ Take the Wand and Try Yourself
Create a file
file_io.py.Write a function
save_list(filename, items)that:Opens
filenamefor writing.Writes each item from
itemslist on its own line.Closes the file.
Write a function
load_list(filename)that:Tries to open
filenamefor reading.Reads lines into a list (stripping whitespace).
Returns the list, or an empty list if the file is missing.
In
if __name__=='__main__'::Call
save_list('names.txt', ['Alice', 'Bob', 'Charlie']).Call
load_list('names.txt')and print the returned list.
Solution Example (file_io.py):
# file_io.py
def save_list(filename, items):
with open(filename, 'w') as f:
for item in items:
f.write(item + '\n')
def load_list(filename):
try:
with open(filename, 'r') as f:
return [line.strip() for line in f]
except FileNotFoundError:
return []
if __name__ == '__main__':
save_list('names.txt', ['Alice', 'Bob', 'Charlie'])
names = load_list('names.txt')
print(names)Expected output:
['Alice', 'Bob', 'Charlie']Run:
python file_io.pyOnce you see the list printed and no errors occur, youβve mastered file I/O and exception handling!
Up next: Day 16: Working with Packages & Virtual Envs