Comprehensive Guide to Python File Operations, Paths, CSV, In-Memory Streams, and Serialization
This article provides a thorough overview of Python file handling, covering opening and closing files, reading and writing modes, absolute and relative paths, file copying, CSV read/write, in‑memory streams with StringIO/BytesIO, sys I/O redirection, and data serialization using JSON and pickle.
This tutorial introduces the essential techniques for working with files in Python, organized into seven sections.
1. Opening and Closing Files
The built‑in open() function opens a file and returns a file handle. Example:
<code>f1 = open(r'd:\测试文件.txt', mode='r', encoding='utf-8')
content = f1.read()
print(content)
f1.close()
</code>Using a context manager automatically closes the file:
<code>with open(r'd:\测试文件.txt', mode='r', encoding='utf-8') as f1:
content = f1.read()
print(content)
</code>Key points: encoding defaults to the OS setting (Windows GBK, Linux/macOS UTF‑8); mode defaults to 'r' . The with statement avoids manual close() and allows handling multiple files in one line.
2. Absolute and Relative Paths
Absolute path: full location, e.g., C:/Users/Python37/python.exe .
Relative path: starts from the current directory, e.g., test.txt , ./test.txt , ../test.txt , demo/test.txt .
Path syntax examples: <code># Windows style file = open('C:\\Users\\Python基础\\xxx.txt') # Raw string file = open(r'C:\Users\Python基础\xxx.txt') # Forward slashes (recommended) file = open('C:/Users/Python基础/xxx.txt') </code>
3. Common File Modes
r – read (default, file must exist).
w – write (creates or truncates).
a – append (creates if missing).
Binary modes: rb , wb , ab .
Combined read/write: r+ , w+ , a+ .
When using r+ , the file pointer starts at the beginning; writing before reading can overwrite existing data.
4. Reading and Writing Data
Reading methods:
read() – reads the whole file (or a specified number of characters/bytes).
readline() – reads one line.
readlines() – returns a list of all lines (may consume much memory).
Iterating over the file object yields lines lazily, saving memory.
<code># Example file content
lucy最帅
lucy很励志
abcdef
哈哈哈
# read whole file
f1 = open('文件操作的读', encoding='utf-8')
content = f1.read()
print(content, type(content))
# read first 6 characters
f1 = open('文件操作的读', encoding='utf-8')
print(f1.read(6)) # lucy最帅
# read line by line
f1 = open('文件操作的读', encoding='utf-8')
print(f1.readline().strip())
print(f1.readline())
# iterate
f1 = open('文件操作的读', encoding='utf-8')
for line in f1:
print(line.strip())
</code>Writing modes:
<code># Write text (creates or truncates)
f1 = open('文件操作的写', encoding='utf-8', mode='w')
f1.write('lucy真帅')
f1.close()
# Write binary data (e.g., copy an image)
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()
</code>Closing a file handle and reopening it in w mode clears its contents.
5. Pointer Positioning
tell() – returns the current offset.
seek(offset, whence) – moves the pointer. whence can be 0 (start), 1 (current), or 2 (end).
<code>f = open('test.txt', 'rb')
print(f.read(3))
print(f.tell())
f.seek(2, 0) # from start, skip 2 bytes
print(f.read())
f.seek(1, 1) # from current, skip 1 byte
print(f.read())
f.seek(-4, 2) # from end, go back 4 bytes
print(f.read())
f.close()
</code>6. File Copy Example
<code>import os
file_name = input('请输入一个文件路径:')
if os.path.isfile(file_name):
old_file = open(file_name, 'rb')
names = os.path.splitext(file_name)
new_file_name = names[0] + '.bak' + names[1]
new_file = open(new_file_name, 'wb')
while True:
content = old_file.read(1024)
if not content:
break
new_file.write(content)
new_file.close()
old_file.close()
else:
print('您输入的文件不存在')
</code>7. CSV File Read/Write
CSV stores tabular data as plain text with commas separating fields.
<code># Write CSV
import 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()
</code>8. In‑Memory Streams
StringIO works like a file object for text data stored in memory:
<code>from io import StringIO
f = StringIO()
f.write('hello\r\n')
f.write('good')
print(f.getvalue())
f.close()
</code>BytesIO does the same for binary data:
<code>from io import BytesIO
f = BytesIO()
f.write('你好\r\n'.encode('utf-8'))
f.write('中国'.encode('utf-8'))
print(f.getvalue())
f.close()
</code>9. sys Module for I/O Redirection
<code>import sys
# Redirect stdout to a file
m = open('stdout.txt', 'w', encoding='utf8')
sys.stdout = m
print('hello')
print('yes')
print('good')
m.close()
# Redirect stderr to a file
x = open('stderr.txt', 'w', encoding='utf8')
sys.stderr = x
print(1 / 0) # raises ZeroDivisionError
x.close()
</code>10. Serialization and Deserialization
JSON converts Python objects to a portable text format:
<code>import json
names = ['zhangsan', 'lisi', 'wangwu', 'jerry']
json_str = json.dumps(names) # -> string
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)
</code>Pickle serializes objects to binary, which is Python‑specific:
<code>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)
</code>JSON is cross‑platform and human‑readable, but only supports basic data types; pickle preserves full Python objects but is not portable across languages.
Source: https://blog.csdn.net/mall_lucy/article/details/104547365
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.