File Cabinet — File handling
Six filing jobs — and the cabinet is drawn after every run, so you can see what you changed.
How to play
Each level starts with some files already in the cabinet, or none at all. Write the code, press Run, and every file is shown with its contents — new ones marked new and edited ones marked changed. File bugs are usually invisible until much later; here you see them immediately.
Nothing touches your real computer. open() inside the game reads and writes an in-memory cabinet,
so even a stray "w" on an important-looking filename is harmless.
What each level teaches
- Write it down —
open(..., "w")and thewithblock that closes it for you. - Read it back —
read()versusreadlines(). - Add, do not replace — mode
"a", and what"w"would have cost you. - Line by line — looping over a file, stripping newlines, skipping blanks.
- Edit the settings —
json.loadandjson.dump. - Total the spreadsheet —
csv.reader, skipping the header, converting text to numbers.
The mode that eats your data
| Mode | If the file exists | If it does not |
|---|---|---|
"r" | Opens for reading | FileNotFoundError |
"w" | Empties it, then writes | Creates it |
"a" | Writes at the end | Creates it |
"x" | FileExistsError | Creates it |
Level 3 is built entirely around the second row. Opening a log file with "w" to add one line
deletes everything already in it, and there is no undo. That is why the level blocks "w" outright.
Always use with
with open(...) as f: closes the file when the block ends, including when an exception is raised
part way through. Without it, data you have written may still be sitting in a buffer rather than on disk, and on a
long-running program you slowly run out of file handles. There is no good reason to open a file any other way.
Text is always text
Everything read from a file is a string. A CSV cell containing 40 is "40", and
"40" + "25" is "4025" rather than 65. Level 6 exists to make that mistake
once, cheaply. More in the Python file handling tutorial.
Frequently asked questions
What does the with statement do when opening a file?
It closes the file automatically when the block ends, even if an exception is raised inside it. That flushes anything still buffered and releases the file handle.
What is the difference between w and a mode in Python?
"w" empties an existing file before writing, so anything already in it is lost. "a" appends to the end and keeps the existing contents. Use "a" for logs.
How do I read a file line by line in Python?
Loop over the file object directly: for line in f. Each line still has its trailing newline, so call .strip() on it before using the text.
How do I read and write JSON files?
json.load(f) reads an open file into Python objects and json.dump(data, f) writes them back. The string versions are json.loads and json.dumps, with the extra s.
Does this game write files to my computer?
No. open() inside the game reads and writes an in-memory cabinet that exists only for that level, so nothing on your machine is touched.
Keep going
- Read the file handling tutorial — the written explanation behind every level here.
- Variable Vault — variables & data types.
- Operator Arcade — numbers & operators.
- String Surgeon — strings & slicing.
- 20 Python practice exercises with worked solutions.
Take the games with you
Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.
Get it on Google Play