liveBook · Manning

Plain text

As explained in Python How-To

A text file is a persistent file that stores character data in an encoding intended to represent textual content.

Plain text is character data stored persistently in an encoded file. Python projects commonly read it programmatically so they can process data across many files, rather than relying on specialized software. section 11.1 section 11.1.1

Read task data as a string

A task-management file such as tasks.txt can contain rows like `1001,Homework,5`. Opening it in read mode and calling `read()` returns all file content as a Python `str`, which can then be split or otherwise processed. section 11.1.1

The built-in `open` function creates a text stream, commonly represented by an `_io.TextIOWrapper`. Its mode controls permitted operations: `r` reads without allowing writes. Close the file after processing, preferably with `with`, which releases the shared resource automatically when the block ends. section 11.1.1

Listing 11.2 Using with to open a file
with open("tasks.txt") as file:     #1
    print(f"file object: {file}")
    data = file.read()
    print(data)
 
# output the following lines:
file object: <_io.TextIOWrapper name='tasks.txt' mode='r' encoding='UTF-8'>
1001,Homework,5
1002,Laundry,3
1003,Grocery,4

Use the complete `with open(...)` example to see a readable pattern that opens tasks.txt, reads its contents, prints the result, and closes the file automatically. section 11.1.1

How Python How-To covers it

How deep it goes
Explains opening, reading, encoding, closing, and context-managed cleanup, while deferring detailed processing methods to other techniques.
Examples
Uses a tasks.txt file containing comma-separated task identifiers, titles, and urgency levels.
What you can do after
Operate/configure Python file access with `open`, `read`, `close`, and `with` for safely processing textual data.
How it connects
Connects plain text with file handling, context managers, strings, encodings, and binary files.

Also explained in

Compare how 2 books explain plain text →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.