Don't leave the books in memory only: Python file input and output
A program that only keeps its books in memory loses them when it closes. Following Chapter 7 of the official Python Tutorial and the csv module docs: f-strings and format, str vs repr, open modes r/w/a/r+, with, five ways to read, write and tell/seek, JSON round-trips, and CSV with DictReader/DictWriter — plus a 100-mistake breakdown from a fictional bookstore script.

First, the boundaries of this guide. This is the fifth guide in the "Python Basics" series, continuing from the first four: the first covered variables, types and control flow, the second functions and modules, the third classes and inheritance, and the fourth exception handling. This one is about file input and output: delivery notes and daily reports cannot live only in memory, or they vanish the moment the program closes. The "Morning Light Bookstore" used below is still the same fictional practice project: a small street-side bookshop whose owner writes a little bookkeeping script that can already work out stock, apply discounts, manage books with classes, and catch errors. The new problem here is saving the books to files so they survive until next time, and reading delivery notes in from files instead of typing them by hand. Everything said here about file input and output comes from Chapter 7 of the official Python Tutorial, "Input and Output", and the official csv module documentation; sources are listed in each guide's meta information and evidence record. Every Morning Light Bookstore operation, code sample and number in this guide was invented to explain the technique and is not a real record. By the end you should be able to do five things: lay numbers out in neat tables with f-strings and format; know what each of open's four modes — r, w, a, r+ — actually does; read and write files with with, and know when a file is closed; save a pile of dictionaries as JSON and read them back; and read and write headered table files with the csv module. The boundary with the fourth guide is this: there, with and open were only about "tidying up" — a file is always closed when you are done; here they are about "reading and writing itself" — the ways to read, the formats to write, the structures to store — with exception handling mentioned only where needed, not re-taught.
Step 1: Make the text look tidy first — f-strings
Chapter 7 of the official tutorial opens by saying a program's output can be presented in several ways: data can be printed in human-readable form, or written to a file for future use. You already know the most common way: expression statements and print(). But print() alone is not enough — a bill has to be laid out neatly to be readable. The first layout tool is the formatted string literal, the f-string: put an f or F before the opening quote, and Python expressions can go straight inside curly braces:
>>> year = 2016
>>> event = 'Referendum'
>>> f'Results of the {year} {event}'
'Results of the 2016 Referendum'
After the braces you can add a format specifier to control how a number looks. The official example rounds pi to three decimal places:
>>> import math
>>> print(f'The value of pi is approximately {math.pi:.3f}.')
The value of pi is approximately 3.142.
:.3f means: fixed-point format, three places after the decimal point. An integer after the colon sets a minimum field width, which is how columns line up. The official example is a phone table:
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
... print(f'{name:10} ==> {phone:10d}')
...
Sjoerd ==> 4127
Jack ==> 4098
Dcab ==> 7678
{name:10} gives the name at least 10 characters, padding on the right with spaces; {phone:10d} formats the phone number as an integer at least 10 characters wide, padding on the left. The arrows line up. The bookstore version: the owner's daily report lists each book's price and stock, one per line, prices to one decimal place:
books = [('The Three-Body Problem', 59.0, 7), ('Twenty Thousand Leagues Under the Seas', 39.5, 12), ('To Live', 29.0, 5)]
for title, price, stock in books:
print(f'{title:38} price {price:5.1f} stock {stock:2d}')
The Three-Body Problem price 59.0 stock 7
Twenty Thousand Leagues Under the Seas price 39.5 stock 12
To Live price 29.0 stock 5
Two small f-string tricks are handy for debugging. !r applies repr() first, so a string appears with its quotes; = prints the expression itself as well, as a built-in label:
>>> animals = 'eels'
>>> print(f'My hovercraft is full of {animals!r}.')
My hovercraft is full of 'eels'.
>>> bugs = 'roaches'
>>> count = 13
>>> area = 'living room'
>>> print(f'Debugging {bugs=} {count=} {area=}')
Debugging bugs='roaches' count=13 area='living room'
My judgement is this: for bills and logs meant for people to read, reach for f-strings first — the expression sits right inside the braces, so anyone reading the code can see at a glance what goes in each slot; save format() for when the format string itself has to be stored and filled in later, which is the next step.
Step 2: format() and manual layout — fill the format string in later
The second tool is the string format() method: the braces sit empty waiting to be filled, and the values are passed in when the method is called. The official examples:
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"
>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam
A number in the braces picks by position; a name picks by keyword argument; positions and keywords can be mixed:
>>> print('This {food} is {adjective}.'.format(
... food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.
>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
... other='Georg'))
The story of Bill, Manfred, and Georg.
Format specifiers work the same as in f-strings. The official example is a vote count: the number right-aligned in 9 characters, the percentage to two decimal places:
>>> yes_votes = 42_572_654
>>> total_votes = 85_705_149
>>> percentage = yes_votes / total_votes
>>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage)
' 42572654 YES votes 49.67%'
{:2.2%} multiplies the fraction by 100, keeps two decimal places, and adds a percent sign, so 0.4967… prints as 49.67%. Note that the underscores in 42_572_654 are only there to make the number easy for people to read — Python treats it as 42572654. The tutorial also prints a table of squares and cubes from 1 to 10 with format():
>>> for x in range(1, 11):
... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
The third tool is doing the string handling yourself: strings have rjust(), ljust() and center() to pad a string to a given width, and zfill() pads a numeric string with zeros on the left. The official examples:
>>> for x in range(1, 11):
... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
... # Note use of 'end' on previous line
... print(repr(x*x*x).rjust(4))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'
Two things to note: rjust() and friends do not change the original string, they return a new one; and if the string is already longer than the width, it is returned unchanged rather than truncated — the tutorial says a messed-up column layout is usually better than lying about a value. To truncate, add a slice yourself. There is also an old style using the % operator, which the tutorial mentions in section 7.1.4:
>>> print('The value of pi is approximately %5.3f.' % math.pi)
The value of pi is approximately 3.142.
It is common in old code; new code should prefer f-strings. My judgement is: do not memorise all three layout tools, just remember the division of labour — f-strings for everyday work, format() when the format has to be stored and reused, zfill() for zero-padded numbers such as an order number like '00012'.
Step 3: str and repr — one for people, one for the machine
Before laying anything out, tell two functions apart: str() and repr(). The tutorial says str() returns a representation that is fairly human-readable, while repr() is meant to generate a representation that can be read by the interpreter — or will force a SyntaxError if there is no equivalent syntax. Many values look the same either way, but strings do not:
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print(s)
The value of x is 32.5, and y is 40000...
>>> hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, world\n'
>>> repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"
repr('hello, world\n') writes the newline as a visible \n and wraps the whole thing in quotes, printing 'hello, world\n' instead of actually starting a new line. That difference is useful when debugging: whether a string ends with a newline, or how many spaces it has, is invisible to print() and obvious to repr(). The bookstore example: one line of the delivery file looks wrong, and the owner wants to know what is really in it — print repr() first:
>>> line = 'The Three-Body Problem,59.0,7\n'
>>> print(line)
The Three-Body Problem,59.0,7
>>> print(repr(line))
'The Three-Body Problem,59.0,7\n'
print(line) leaves an extra blank line, because the \n at the end of the string starts a new line of its own; repr(line) shows the \n as it is, and the puzzle is solved at once. My judgement is: use str() and f-strings for daily reports meant for people; use repr() for questions like "what exactly did I read from this file". The tutorial also mentions string.Template, a simpler templating approach with $x placeholders and less control — enough to know it exists.
Step 4: open() — four modes, learn them before you touch a file
File input and output starts with open(). Section 7.2 of the tutorial: open() returns a file object, and is most commonly used with two positional arguments, open(filename, mode):
>>> f = open('workfile', 'w')
The first argument is the filename, the second is a mode string. There are four modes, and their meanings are worth memorising: 'r' reads only and raises an error if the file does not exist; 'w' writes only, and an existing file with the same name will be erased — the most dangerous one, and the top entry in this guide's mistake chart in step 10; 'a' appends, so anything written is automatically added to the end; 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' is assumed if it is omitted. Normally files are opened in text mode, so you read and write strings, encoded in a specific encoding — if no encoding is given, the default is UTF-8. Appending a 'b' to the mode opens the file in binary mode, where data is read and written as bytes objects and no encoding can be specified. In text mode, reading converts platform-specific line endings (\n on Unix, \r\n on Windows) to just \n, and writing converts them back. That behind-the-scenes change is fine for text files, but will corrupt binary data like JPEG or EXE files — always use binary mode for reading and writing those.
The bookstore example: the delivery note is called stock.csv and the daily report daily.txt. Read the delivery note with the default 'r', write the report with 'w', and add one new record with 'a'. The first time the report is written, 'w' creates a fresh empty file; writing the same report again the next day with 'w' wipes yesterday's — to keep yesterday's, either use a new filename or append with 'a'. My judgement is: before writing to a file, ask yourself one question — "do I still need what is already in this file?" If yes, use 'a'; only use 'w' if the answer is no; if you are not sure, read it first with 'r'.
Step 5: with — the file is always closed when you are done
The tutorial's good practice is to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point — and it is much shorter than writing the equivalent try/finally blocks:
>>> with open('workfile') as f:
... read_data = f.read()
>>> # We can check that the file has been automatically closed.
>>> f.closed
True
Without with, you should call f.close() to close the file and immediately free up the system resources it uses. The tutorial also warns: calling f.write() without using with or calling f.close() might result in the arguments of f.write() not being completely written to the disk, even if the program exits successfully. After a file object is closed, attempts to use it will automatically fail:
>>> f.close()
>>> f.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
Guide 4 called with "a finally built in", and this is where it really earns its keep: reading files, writing files, all as with open(...) as f. My judgement is: this one is not negotiable — write every file-opening as a with, and never rely on remembering to close the file. The exception-handling rules from guide 4 do not need repeating here: with only handles the tidy-up; a missing file or bad content still needs its own try where appropriate.
Step 6: Five ways to read a file — all at once, or one line at a time
File objects have several read methods, covered one by one in section 7.2.1. f.read(size) reads some quantity of data and returns it as a string in text mode or a bytes object in binary mode; when size is omitted or negative, the entire contents are read and returned — it is your problem if the file is twice as large as your machine's memory; when the end of the file has been reached, f.read() returns an empty string (''):
>>> f.read()
'This is the entire file.\n'
>>> f.read()
''
f.readline() reads a single line; a newline character (\n) is left at the end of the string, and is only omitted on the last line if the file does not end in a newline. That makes the return value unambiguous: an empty string means the end of the file has been reached, while a blank line is represented by '\n', a string containing only a single newline:
>>> f.readline()
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
>>> f.readline()
''
For reading lines from a file, you can loop over the file object itself. This is memory efficient, fast, and leads to simple code:
>>> for line in f:
... print(line, end='')
...
This is the first line of the file.
Second line of the file
To read all the lines into a list, use list(f) or f.readlines(). The bookstore example: the delivery note stock.csv has only three rows, so reading it whole is fine; but ten years of the owner's transaction log runs to hundreds of thousands of lines, and reading that whole thing into memory would hurt — read it one line at a time. The code below uses a temporary file; the numbers are a fictional example:
with open('stock.csv', encoding='utf-8') as f:
lines = f.readlines()
# lines is ['title,price,stock\n', 'The Three-Body Problem,59.0,7\n', 'Twenty Thousand Leagues Under the Seas,39.5,12\n', 'To Live,29.0,5\n']
# every line ends with '\n'; strip it off with line.strip() before splitting
with open('stock.csv', encoding='utf-8') as f:
for line in f:
title, price, stock = line.strip().split(',')
print(title, float(price), int(stock))
The Three-Body Problem 59.0 7
Twenty Thousand Leagues Under the Seas 39.5 12
To Live 29.0 5
Watch out for one easy trap: read() pushes the read position to the end of the file, so calling read() again returns the empty string '', not "the file one more time". To read again, move the position back first — that is seek(), in step 8. My judgement is: small files get read() or readlines(), big files get for line in f; read one line and deal with it, instead of hauling the whole file into memory first.
Step 7: Writing a file — write returns how many characters it wrote
f.write(string) writes the contents of a string to the file, returning the number of characters written:
>>> f.write('This is a test\n')
15
'This is a test\n' is 14 letters plus one newline, 15 characters in all. Other types of objects need to be converted first — to a string in text mode, or a bytes object in binary mode:
>>> value = ('the answer', 42)
>>> s = str(value) # convert the tuple to string
>>> f.write(s)
18
str(('the answer', 42)) is "('the answer', 42)", 18 characters. Calling f.write(42) directly raises an error: write() only takes a string. The bookstore example: to write the daily report, build each line as a string first, then write it:
total = 59.0 * 7 + 39.5 * 12 + 29.0 * 5 # 1032.0
with open('daily.txt', 'w', encoding='utf-8') as f:
f.write('Morning Light Bookstore daily report\n')
f.write(f'Total stock value: {total:.1f}\n')
f.write('Titles in stock: 3\n')
The total stock value of the three books is 1032.0: 59.0 times 7 is 413.0, 39.5 times 12 is 474.0, 29.0 times 5 is 145.0, and they add up to 1032.0. Note that write() does not add a newline by itself — put \n at the end of the string yourself. print() adds one automatically; write() does not, and that is the easiest line to forget.
Step 8: tell and seek — the pointer inside the file
A file object carries an invisible pointer recording where you have read or written up to. f.tell() returns the file object's current position: the number of bytes from the beginning of the file in binary mode, and an opaque number in text mode. To change the position, use f.seek(offset, whence): the position is computed from adding offset to a reference point, and the reference point is selected by whence — 0 measures from the beginning of the file, 1 uses the current position, and 2 uses the end of the file; whence can be omitted and defaults to 0. The official example uses binary mode:
>>> f = open('workfile', 'rb+')
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5) # Go to the 6th byte in the file
5
>>> f.read(1)
b'5'
>>> f.seek(-3, 2) # Go to the 3rd byte before the end
13
>>> f.read(1)
b'd'
'rb+' is binary mode for both reading and writing. After writing 16 bytes, the pointer moves to byte 5 (counting from 0, the 6th byte), and reading one byte gives b'5'; moving 3 bytes back from the end lands at position 13, and reading one byte gives b'd'. In text files, only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2)), and the only valid offset values are those returned from f.tell(), or zero — any other offset value produces undefined behaviour. The bookstore example: after writing the daily report, jump back to the start to read the first line and check it:
with open('daily.txt', 'w+', encoding='utf-8') as f:
f.write('Morning Light Bookstore daily report\n')
f.seek(0)
first_line = f.readline()
# first_line is 'Morning Light Bookstore daily report\n'
My judgement is: you will not need tell() and seek() every day, but know they exist — the trap from step 6, "read it once and the second read is empty", is fixed by seek(0) back to the start; and when you read a file line by line from top to bottom, you never need to think about the pointer at all.
Step 9: JSON — save a whole pile of data, read the whole pile back
Strings are easy to write to a file and read back. Numbers take a bit more effort, since read() only returns strings, which have to be passed to a function like int(). When you want to save more complex data types like nested lists and dictionaries, parsing and serializing by hand becomes complicated. Section 7.2.2 of the tutorial offers JSON, a popular data interchange format: the standard module called json can take Python data hierarchies and convert them to string representations — this process is called serializing — and reconstructing the data from the string is called deserializing. Between serializing and deserializing, the string may have been stored in a file or sent over a network connection to some distant machine. The official example:
>>> import json
>>> x = [1, 'simple', 'list']
>>> json.dumps(x)
'[1, "simple", "list"]'
dumps() turns an object into a JSON string; another variant, dump(), serializes the object straight into a text file opened for writing:
json.dump(x, f)
To decode the object again, use load():
x = json.load(f)
Note that JSON files must be encoded in UTF-8, which is the default encoding for text files. The bookstore example: save the whole stock list as JSON, and the next time the script opens, read it back and carry on — no more splitting strings line by line:
import json
stock = [
{'title': 'The Three-Body Problem', 'price': 59.0, 'stock': 7},
{'title': 'Twenty Thousand Leagues Under the Seas', 'price': 39.5, 'stock': 12},
{'title': 'To Live', 'price': 29.0, 'stock': 5},
]
with open('stock.json', 'w', encoding='utf-8') as f:
json.dump(stock, f, ensure_ascii=False)
with open('stock.json', encoding='utf-8') as f:
loaded = json.load(f)
# loaded is identical to stock: all three books' titles, prices and stock are there
# json.dumps(stock, ensure_ascii=False) shows the titles in plain text
ensure_ascii=False keeps non-ASCII text written as itself in the file instead of escaped as \uXXXX. The loaded value is a list of dictionaries, and the numbers are still numbers: loaded[0]['price'] * loaded[0]['stock'] is 413.0 straight away, with no conversion. The tutorial adds one more note: the pickle module can serialize arbitrarily complex Python objects, but it is specific to Python and insecure by default — deserializing pickle data from an untrusted source can execute arbitrary code. My judgement is: for storing data and exchanging it, prefer JSON — other languages understand it, and a person can open the file and read it; only consider pickle when you are certain the data was written by you and will only ever be read by you.
Step 10: CSV — table files have their own way of being read, plus a mistake chart
The last tool is the table file. CSV — Comma Separated Values — is the most common import and export format for spreadsheets and databases. Python's standard csv module implements classes to read and write tabular data in CSV format: reader and writer work with rows as lists, and DictReader and DictWriter read and write rows as dictionaries. The official documentation has one rule that matters most: if the CSV file is a file object, it should be opened with newline='', or extra blank lines will appear between rows on Windows. The reading example:
import csv
with open('eggs.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
print(', '.join(row))
Each row comes back as a list of strings, with no automatic data type conversion — the price column reads back as the string '59.0', and you convert it yourself with float(). Writing works the same way: all non-string data is stringified with str() before being written, and None is written as the empty string. The bookstore example: the delivery note is a CSV with a header row, so DictReader reads it and DictWriter writes it, with column names as dictionary keys:
import csv
# Read the delivery note: the first row is the header title,price,stock
with open('stock.csv', newline='', encoding='utf-8') as f:
reader = csv.DictReader(f)
rows = list(reader)
# rows is [{'title': 'The Three-Body Problem', 'price': '59.0', 'stock': '7'}, ...], 3 rows
# note that price and stock come back as strings: float('59.0') is 59.0, int('7') is 7
# Write the delivery note: header first, then one row at a time
with open('stock.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=['title', 'price', 'stock'])
writer.writeheader()
writer.writerow({'title': 'The Three-Body Problem', 'price': 59.0, 'stock': 7})
writer.writerow({'title': 'Twenty Thousand Leagues Under the Seas', 'price': 39.5, 'stock': 12})
writer.writerow({'title': 'To Live', 'price': 29.0, 'stock': 5})
Finally, the whole guide in one workflow: the owner's daily routine in three steps — read the delivery CSV, work out the total stock value, write the daily report as TXT and back the stock up as JSON:
import csv
import json
with open('stock.csv', newline='', encoding='utf-8') as f:
rows = list(csv.DictReader(f))
total = 0.0
for row in rows:
total += float(row['price']) * int(row['stock'])
# total is 1032.0
with open('daily.txt', 'w', encoding='utf-8') as f:
f.write('Morning Light Bookstore daily report\n')
f.write(f'Titles in stock: {len(rows)}\n')
f.write(f'Total stock value: {total:.1f}\n')
with open('stock.json', 'w', encoding='utf-8') as f:
json.dump(rows, f, ensure_ascii=False)
Three rows of delivery note, and the total still comes out as 1032.0, matching the hand calculation in step 7. Note that json.dump(rows, ...) stores the CSV version — price is the string '59.0', not the number 59.0; to have numbers in the JSON too, convert each row first: {'title': row['title'], 'price': float(row['price']), 'stock': int(row['stock'])}. That small conversion is exactly what step 9 meant by "CSV does not convert types automatically".
Finally, this guide's mistake chart. Example: the fictional Morning Light Bookstore script, 100 file input/output mistakes in its fifth month of learning Python, broken down below. The top mistake is opening an old file with 'w' and wiping the data inside it.

Looking back over the ten steps, there are really only three things in this guide. First, lay output out before anything else: f-strings for bills meant for people, format() for formats to fill in later, repr() for checking what a file actually contains. Second, know the mode before opening a file: 'r' reads, 'w' writes (and erases), 'a' appends, 'r+' does both; open everything with with so you never have to remember to close it. Third, pick the read by size and the storage by purpose: read small files whole, big files one line at a time; store whole datasets as JSON and tables as CSV, and remember newline='' when opening a CSV. The next guide is about list comprehensions and generators: the delivery note comes in as a pile of dictionaries, and a job like "pick out the books with fewer than 10 copies in stock" takes one line of comprehension — the rows read in here will become the raw material for that guide.
Comments (0)
Sources
Related articles
A list comprehension says what it wants first, and a generator gives back one item at a time. Following Chapters 5 and 9 of the official Python Tutorial: list, dict and set comprehensions, double for, nested comprehensions, * unpacking, yield, generator functions, generator expressions, and when to pick a list over a generator — plus a 100-mistake breakdown from a fictional bookstore script.
An exception is not a broken program: it is Python telling you what went wrong, with a name you can catch. Following Chapter 8 of the official Python Tutorial: syntax errors vs exceptions, try/except, matching by inheritance, which errors to catch and which to leave alone, raise, else, finally, with, user-defined exceptions, chaining, and ExceptionGroup — plus a 100-accident breakdown from a fictional bookstore script.
A class bundles data and its rules together: one Book class gives every book its own state, methods let objects do the work themselves, and inheritance lets an e-book stand on a paper book's shoulders. Following Chapter 9 of the official Python Tutorial: class and __init__, self, methods, class vs instance variables, attribute lookup, inheritance and overriding, super(), the underscore-private convention, dataclasses, __iter__ and __next__ — plus a 100-accident chart.