7 File Input and Output
Learn how to read, write, and manage text files in Python using safe file handling, portable paths, explicit encodings, and targeted exception handling.
Choose the right file mode
Python uses files to preserve information beyond one program run. Reading obtains existing data, while writing creates or changes stored data. The central operation is , which returns a .
The general form is open(file, mode="r", =None). The mode determines what the program may do:
"r"reads an existing file and is the default."w"writes a file, replacing its existing contents and creating it if necessary."a"appends new data to the end, preserving existing contents."x"creates a new file and fails if that file already exists."r+"permits reading and writing without automatically truncating the file.
Adding "b", as in "rb" or "wb", selects binary mode. Text mode works with strings, while binary mode works with bytes. Do not provide a text for binary mode.
Read text efficiently
For small text files, read() obtains the remaining contents as one string. A size argument limits how much is read, and a later call continues from the file’s current position. Once the end is reached, another read() returns an empty string.
readline() obtains one line at a time, normally including its ending newline. Calling strip() removes surrounding whitespace, including that newline. A can also be iterated directly:
Iterate over the file when processing a large file line by line.
Use
list(file)orreadlines()when a list containing every line is needed.Avoid reading an entire very large file at once when line-by-line processing is sufficient.
For example, a loop can strip each line, convert it with int(), and process the resulting value without loading all lines into memory.
Write, replace, or append
The write() method writes a string and returns the number of characters written. It does not automatically add a newline, so include one when separate output lines are required. Non-string values must be converted before writing, or inserted into a formatted string with an f-string.
The most important safety distinction is between replacement and preservation:
Opening with
"w"truncates the file before writing, so existing contents are erased.Opening with
"a"preserves existing contents and writes after them.Opening with
"x"prevents accidental overwriting by failing when the file already exists.
Before choosing "w", decide explicitly whether replacing the previous contents is intended.
Build portable file paths
A relative path is interpreted from the program’s current working directory, which may differ from the directory containing the Python source file. An absolute path specifies its complete location.
provides a portable way to construct and inspect paths. For example, joining a directory and filename with the / operator avoids manually choosing platform-specific separators. A Path object can be passed to , or its , read_text(), and write_text() methods can be used directly.
Use read_text() and write_text() for simple one-step text operations. Use path. when you need iteration, several operations, or more control over the .
Takeaway: Build paths with Path objects rather than assembling operating-system separators manually.
Close files and handle failures
A file uses operating-system resources and may buffer output. The preferred pattern is a with statement, which creates a and closes the file automatically when execution leaves the indented block, including when an exception occurs.
For predictable text handling, specify ="utf-8". Text mode translates platform-specific line endings for ordinary text files; this behavior is not appropriate for binary formats such as images or executable files.
File operations can fail for expected reasons. Catch specific exceptions when possible:
Handle
when a requested file is absent.Handle
when access is not allowed.Handle
when the selected cannot decode the file’s bytes.
Avoid a broad except Exception when a narrower exception identifies the expected problem. Specific handlers make failures clearer and reduce the risk of hiding unrelated programming errors.
Takeaway: Combine with, an explicit , and targeted exception handling for reliable text-file programs.