Coding Python
Home›Python Coding Games›File Cabinet

File Cabinet — File handling

Six filing jobs — and the cabinet is drawn after every run, so you can see what you changed.

6 levels · Intermediate
Level 1

Checks
    Your PythonPython 3
    1

    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

    1. Write it down — open(..., "w") and the with block that closes it for you.
    2. Read it back — read() versus readlines().
    3. Add, do not replace — mode "a", and what "w" would have cost you.
    4. Line by line — looping over a file, stripping newlines, skipping blanks.
    5. Edit the settings — json.load and json.dump.
    6. Total the spreadsheet — csv.reader, skipping the header, converting text to numbers.

    The mode that eats your data

    ModeIf the file existsIf it does not
    "r"Opens for readingFileNotFoundError
    "w"Empties it, then writesCreates it
    "a"Writes at the endCreates it
    "x"FileExistsErrorCreates 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.

    Practise file handling on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    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

    ← All Python coding games  Read the tutorial →

    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
    Coding Python app icon