W WolfCode · CSC 141

Walkthrough: Write Then Read

例题: 写入再读取

≈ 8 min · python-file-io · Open in WolfCode

Three tiny functions — write a file, read it back as a string, read it as a list of lines. The `with` statement guarantees the file gets closed.

The three functions

Python · runnable

Three modes

ModeEffect on existing fileUse case
"r"(default) read-only, opens at startRead existing data
"w"erases then writesOverwrite outputs
"a"appends to the endLogs, audit trails