File reading and writing
#File reading and writing As one of the most common IO scenarios in daily Python development, file reading and writing may seem simple, but the details can determine the robustness, efficiency and even security of the code. Today we will use a short article to explain it clearly, from basic APIs to pitfall avoidance guides!
Basics of file operations
Python uses the built-inopen()The function performs core file operations and returns a file object/file descriptor - this is like a "bridge" between the Python code and the operating system disk file.
For security and stability reasons, modern operating systems prohibit programs from directly reading and writing disk sectors, and all file operations must be packaged through the unified API provided by the system. This is also why we can't simply read a file using a memory address: we have to let the operating system do the actual I/O work for us.
Read file
Basic opening and exception-handling
useopen()When reading a file, the default mode is'r'(text reading). But if the path you give does not exist, Python will throw out without mercyFileNotFoundErrorInterrupt the program:
In order to make the program more robust, be sure to add the most basic exception catching:
Four common ways to read content
After successfully opening the file, select the corresponding method based on the file size and reading requirements:
-
Read all at once – suitable for small files (usually within tens of KB)
-
Read segments by bytes/characters – Friendly for large files, avoiding one-time loading that fills up memory
-
Line-by-line single reading – commonly used when logical processing needs to be performed on each line
-
Read all into the list – very convenient when you need to operate the row content multiple times later
Two methods to safely release resources
After the file is opened, it will occupy the file descriptor resources of the operating system (the number available for each process is limited, such as the default number of Linux is usually 1024), and it must be closed in time when used up!
-
Manual close – easy to forget, and will be skipped when reading or writing throws an exception
close() -
withAutomated management (highly recommended) – Leverage context managers to automatically trigger at the end of a code blockclose(), it can be closed correctly even if an exception occurs midway
File object type
Python supports three core types of "file-like" objects. Understanding their usage scenarios can save you a lot of detours:
-
Disk text file The default opening method, processing by character/string, will automatically decode and encode.
-
Disk Binaries Process non-text content such as images, videos, compressed packages, etc. by bytes/byte strings.
-
Memory file Simulates file operations entirely in memory, requiring no real disk I/O, making it extremely fast and ideal for working with temporary data.
Process text files with different encodings
Python 3 uses UTF-8 encoding to read and write text by default, but on Windows, old files of GBK/GB2312 are often encountered, and opening them directly will throw an error message.UnicodeDecodeError:
If there are a small number of illegal characters (such as mixed garbled characters) in the file, you can useerrorsParameter adjustment processing strategy:
errors='ignore'– Ignore illegal characterserrors='replace'- use�replace illegal characterserrors='strict'– Default value, report an error directly (safest, reminding you that there is a problem with the file)
Write to file
Two basic writing modes
The most critical thing about writing operations is to prevent accidental overwriting of original data. Let’s first look at the two most commonly used modes:
-
Overwrite (
'w')
If the file does not exist, it will be automatically created; if the file already exists, it will be opened and clear all contents and then start writing from scratch. -
Additional writing (
'a')
If the file does not exist, it will be automatically created; if it already exists, it will be written from the end of the file and the original content will be retained.
Two ways to write multiple lines
You can choose to call in a loopf.write(), you can also use the more concisef.writelines():
⚠️ Note:f.writelines()Line breaks will not be added automatically! You have to manually splice it yourself\n。
File Mode Cheat Sheet
Examples of common combinations:
'rb'– Read binary files such as images and compressed packages'w+'– Read and write (overwriting type, you need to manually move the file pointer after reading to re-read)'a+'– Reading and writing (append type, the pointer must also be moved after reading)
Avoid pitfalls and best practices
- Always added by default
encoding='utf-8'
Avoid encoding issues when cross-platform (Windows ↔ Linux / macOS). - Use forever
withStatement Forgetting to close the file manually is the most common mistake novices make. UsewithOnce and for all. - Large files must be iterated line by line/section by section
For example, to process GB-level log files, use
for line in f:Line by line reading ratiof.readlines()100 times more memory friendly: - use
'x'Schema avoids overwriting critical data For example, when writing a user configuration file, prevent the old configuration from being accidentally deleted:
Quick exercise: Reading system time zone file
Use the knowledge you just learned to write a small script to read commonly used time zone files in Linux / macOS:
Summarize
Today we have sorted out the core knowledge points of Python file reading and writing:
- Use
open()CooperatewithContext manager safely opens and closes files - Distinguish the usage scenarios of three file objects: text, binary and memory.
- Handle encoding issues (
encodinganderrorsparameter) - Three basic writing modes: overwrite, append, and exclusive creation
- Memory-friendly tips for large file processing
Master these, and 90% of the file I/O needs in daily development can be easily solved! In the next article, let’s talk about more advanced CSV/JSON/Excel structured file reading and writing~

