Technical skill practice

Python Read File: UTF-8 Text, Lines and Newline Handling

Read a text file in Python with an explicit encoding and a context manager so the handle closes when the block ends. read returns the remaining text, while iterating the handle produces lines. Decide whether line-ending characters should remain; stripping all whitespace is a different transformation.

Start with a small, explicit task

Create a temporary fictional note with an accented label and meaningful trailing spaces on its second line. Read the complete text, then read lines separately and remove only newline characters. The temporary fixture is deleted automatically, and no existing user file is opened.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_read_file.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

'café\nlabel \n'

['café', 'label ']

from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory(prefix="text-read-") as folder:
    path = Path(folder) / "note.txt"
    content = "café\nlabel  \n"
    path.write_text(content, encoding="utf-8")
    with path.open("r", encoding="utf-8") as handle:
        whole = handle.read()
        assert handle.read() == ""
    assert handle.closed
    with path.open("r", encoding="utf-8") as handle:
        lines = list(handle)
    cleaned = [line.removesuffix("\n") for line in lines]
    assert whole == content
    assert lines == ["café\n", "label  \n"]
    assert cleaned == ["café", "label  "]
    print(repr(whole))
    print(cleaned)

Explain the result

The first read consumes the handle, so a second read on that same handle returns an empty string at the end. Opening the fixture again creates a fresh position for line iteration. The context manager closes each handle, including the first one whose closed state is checked explicitly.

The newline remains attached to each iterated line. removesuffix removes that specific ending while preserving the two spaces after label. A generic strip would also delete those spaces, which would violate the stated fixture requirement. The explicit UTF-8 encoding makes the accented character’s intended interpretation clear.

Avoid these mistakes

read loads the remaining content into memory, so line iteration may be more suitable for a large file. A missing file can raise FileNotFoundError, while decoding and permission failures are different cases; report the actual failure rather than treating every unsuccessful read as missing content.

Questions, answered

Why is a second read empty?

The first read reaches the end of that handle. Reopen or reposition deliberately if you need another pass.

Should I strip every line?

Only when removing all surrounding whitespace is intended. This example removes the newline while preserving trailing spaces.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.