Master Python File I/O: From Basics to Advanced Serialization
This guide walks through Python file operations—including opening, closing, reading, writing, path handling, file copying, CSV handling, in‑memory streams, sys.stdin/stdout/stderr redirection, and JSON vs. pickle serialization—providing code examples and practical tips for each topic.
1. Opening and Closing Files
Use open() to obtain a file handle. The default mode is 'r' (read) and the default encoding follows the operating system (Windows: gbk, Linux/macOS: utf-8). Always close the file with f.close() or use a with statement to close automatically.
open() function
f1 = open(r'd:\测试文件.txt', mode='r', encoding='utf-8')
content = f1.read()
print(content)
f1.close() with open(r'd:\测试文件.txt', mode='r', encoding='utf-8') as f1:
content = f1.read()
print(content)open() internally calls the OS file interface.
The variable (e.g., f1) is the file handle; all operations go through it.
Encoding can be omitted; defaults to the OS encoding.
Mode defaults to 'r'. f1.close() releases the handle.
Using with open() eliminates the need for an explicit close.
Absolute and Relative Paths
Absolute paths describe the full location (e.g., C:/Users/Python37/python.exe). Relative paths start from the current directory: test.txt, ./test.txt (same as current folder), ../test.txt (parent folder), demo/test.txt (sub‑folder). Windows paths can be written with backslashes, raw strings, or forward slashes—the latter is recommended.
2. Reading and Writing
Reading
Common read methods: read() – reads the entire file (text mode) or all bytes (binary mode). read(n) – reads n characters (text) or bytes (binary). readline() – reads a single line. readlines() – returns a list of all lines (use with caution on large files).
Iterating over the file object yields one line at a time, which is memory‑efficient.
f1 = open('文件操作的读', encoding='utf-8')
content = f1.read()
print(content, type(content))
f1.close() f1 = open('文件操作的读', encoding='utf-8')
print(f1.readline().strip()) # first line
print(f1.readline()) # second line with newline
f1.close() f1 = open('文件操作的读', encoding='utf-8')
lines = f1.readlines()
print(lines)
f1.close() f1 = open('文件操作的读', encoding='utf-8')
for line in f1:
print(line.strip())
f1.close()Writing
Modes for writing: w – creates a new file or truncates an existing one. a – appends to an existing file or creates a new one. wb, ab – binary equivalents for non‑text data. + variants (e.g., r+, w+, a+) allow simultaneous reading and writing.
f1 = open('文件操作的写', encoding='utf-8', mode='w')
f1.write('lucy真帅')
f1.close() f1 = open(r'C:\Users\lenovo\Desktop\编码进阶.png', mode='rb')
content = f1.read()
f1.close()
f2 = open('图片.jpg', mode='wb')
f2.write(content)
f2.close()File Pointer Positioning
Use tell() to get the current offset and seek(offset, whence) to move it. whence can be 0 (from start), 1 (from current), or 2 (from end).
f = open('test.txt')
print(f.read(10))
print(f.tell())
f.seek(2, 0) # skip two bytes from start
print(f.read())
f.seek(1, 1) # skip one byte from current position
print(f.read())
f.seek(-4, 2) # four bytes before end
print(f.read())
f.close()3. File Copying
A simple binary copy reads chunks and writes them to a new file with a .bak suffix.
import os
file_name = input('请输入一个文件路径:')
if os.path.isfile(file_name):
old_file = open(file_name, 'rb')
base, ext = os.path.splitext(file_name)
new_file_name = base + '.bak' + ext
new_file = open(new_file_name, 'wb')
while True:
chunk = old_file.read(1024)
if not chunk:
break
new_file.write(chunk)
new_file.close()
old_file.close()
else:
print('您输入的文件不存在')4. CSV Read/Write
CSV files store tabular data as plain text with commas separating fields. The csv module simplifies reading and writing.
import csv
# Write CSV
file = open('test.csv', 'w', newline='')
writer = csv.writer(file)
writer.writerow(['name', 'age', 'score'])
writer.writerows([
['zhangsan', '18', '98'],
['lisi', '20', '99'],
['wangwu', '17', '90'],
['jerry', '19', '95']
])
file.close()
# Read CSV
file = open('test.csv', 'r')
reader = csv.reader(file)
for row in reader:
print(row)
file.close()5. In‑Memory Streams
StringIOhandles text data in memory, while BytesIO works with binary data.
from io import StringIO
f = StringIO()
f.write('hello
')
f.write('good')
print(f.getvalue())
f.close()
from io import BytesIO
b = BytesIO()
b.write('你好
'.encode('utf-8'))
b.write('中国'.encode('utf-8'))
print(b.getvalue())
b.close()6. sys Module Redirection
Redirect standard streams for custom input/output handling.
import sys
# stdin example
s_in = sys.stdin
while True:
line = s_in.readline().rstrip('
')
if line == '':
break
print(line)
# stdout redirection
m = open('stdout.txt', 'w', encoding='utf8')
sys.stdout = m
print('hello')
print('yes')
print('good')
m.close()
# stderr redirection
x = open('stderr.txt', 'w', encoding='utf8')
sys.stderr = x
print(1/0) # raises ZeroDivisionError, written to stderr
x.close()7. Serialization and Deserialization
Convert Python objects to a storable form and back.
JSON
JSON produces a text representation that is language‑agnostic. Use json.dumps() to get a string, json.dump() to write directly to a file, and json.loads() / json.load() to read.
import json
names = ['zhangsan', 'lisi', 'wangwu', 'jerry']
json_str = json.dumps(names)
with open('names.txt', 'w') as f:
f.write(json_str)
# Load back
with open('names.txt', 'r') as f:
data = json.load(f)
print(data)pickle
pickle serializes objects to binary, preserving Python‑specific details but not portable across languages.
import pickle
names = ['张三', '李四', '杰克', '亨利']
binary = pickle.dumps(names)
with open('names.bin', 'wb') as f:
f.write(binary)
# Load back
with open('names.bin', 'rb') as f:
data = pickle.load(f)
print(data)JSON vs. pickle
JSON creates a human‑readable text format; suitable for cross‑platform data exchange.
pickle creates a Python‑specific binary format; can serialize any Python object but is not safe for untrusted data.
Custom objects need a custom JSONEncoder for JSON, while pickle handles them automatically.
Source: 南枝向暖北枝寒MA (https://blog.csdn.net/mall_lucy/article/details/104547365)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
