Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    PyPython

    Getting started

    • Your first Python program
    • Variables and types
    • Numbers and operators
    • Type conversion and input

    Strings

    • Strings and f-strings
    • String indexing and slicing
    • Cleaning and splitting strings

    Control flow

    • Conditions: if, elif and else
    • Loops: for and while
    • Loop control and nesting

    Functions

    • Functions with def
    • Parameters and scope

    Collections

    • Lists
    • Dictionaries
    • Tuples and sets
    • Comprehensions, enumerate and zip

    Errors, files and libraries

    • Handling exceptions
    • Modules and the standard library
    • Reading and writing files
    • Exchanging data with JSON

    Objects and lazy iteration

    • Classes and objects
    • Inheritance and composition
    • Iterators and generators

    Testing and project

    • Testing and project setup
    • Project: a grade report
    Python

    Errors, files and libraries · Lesson 19 of 25

    Reading and writing files

    with, encoding and file lifetime.

    8 minutes2 games

    Use with open(path, mode, encoding="utf-8") as file: so the file closes even if an exception occurs. r reads, w writes from the beginning, and a appends. The example uses a temporary directory; runner files are not persistent storage.

    Python
    from pathlib import Path
    from tempfile import TemporaryDirectory
    
    with TemporaryDirectory() as folder:
        path = Path(folder) / "notes.txt"
        with open(path, "w", encoding="utf-8") as file:
            file.write("Hello\nPython")
        with open(path, "r", encoding="utf-8") as file:
            print(file.read())

    Open file

    Read or write

    Exit with

    File closes

    Important

    Mode w truncates an existing file. Use a to append, or read first when updating existing data.

    Interview question

    Why prefer with over manually calling open and close?

    Quick question

    Which mode appends without truncating?

    Exercise

    Read and print the first line of this in-memory text file without an extra blank line.

    Expected output: Python

    Python
    from io import StringIO
    file = StringIO("Python\nHTML\n")
    

    Learn more from

    • Python documentationdocs.python.org
    • Real Python: further readingrealpython.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousModules and the standard libraryNextExchanging data with JSON
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.