Comprehensive Guide to Python File Operations, CSV, JSON, and Pickle
This article provides a thorough overview of Python file handling, covering opening and closing files, absolute and relative paths, various file modes, reading and writing techniques, pointer manipulation, file copying, CSV I/O, in‑memory streams with StringIO/BytesIO, and serialization/deserialization using JSON and pickle modules.
1. Opening and Closing Files
The built‑in open() function opens a file and returns a file handle (e.g., f1 ). Parameters include the file path, mode (default 'r' ), and encoding (default OS encoding). Always close the handle with f1.close() or use with open() as f to auto‑close.
<code>f1 = open(r'd:\测试文件.txt', mode='r', encoding='utf-8')
content = f1.read()
print(content)
f1.close()</code> <code>with open(r'd:\测试文件.txt', mode='r', encoding='utf-8') as f1:
content = f1.read()
print(content)</code>Advantages of with open()
1) No need to manually close the file. 2) Multiple file handles can be managed in a single statement.
2. Absolute and Relative Paths
Absolute paths specify the full location (e.g., C:/Users/chris/.../python.exe ). Relative paths start from the current directory ( test.txt , ./test.txt , ../test.txt , demo/test.txt ).
3. Common File Access Modes
Text modes: r (read), w (write, truncates), a (append). Binary modes: rb , wb , ab . Combined modes with + allow read/write.
4. Reading and Writing Files
Reading
Methods include read() , read(n) , readline() , readlines() , and iteration over the file object.
<code>f1 = open('文件操作的读', encoding='utf-8')
content = f1.read()
print(content, type(content))
f1.close()</code>Writing
Use write() with modes w (text) or wb (binary). The file is created if it does not exist; existing files are truncated.
<code>f1 = open('文件操作的写', encoding='utf-8', mode='w')
f1.write('lucy真帅')
f1.close()</code>Pointer Positioning
tell() returns the current offset; seek(offset, whence) moves the pointer (whence: 0‑start, 1‑current, 2‑end).
<code>f = open('test.txt', 'rb')
print(f.read(3))
print(f.tell())
f.seek(2, 0)
print(f.read())
f.close()</code>5. File Copying
Read a source file in binary chunks and write to a new file with a .bak suffix.
<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)
new_file.write(content)
if not content:
break
new_file.close()
old_file.close()
else:
print('您输入的文件不存在')
</code>6. CSV Reading and Writing
CSV files store tabular data as plain text. Use the csv module to read/write rows.
<code>import csv
file = open('test.csv', 'w')
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()
</code> <code>import csv
file = open('test.csv', 'r')
reader = csv.reader(file)
for row in reader:
print(row)
file.close()
</code>7. In‑Memory Streams
StringIO handles text streams; BytesIO handles binary streams.
<code>from io import StringIO
f = StringIO()
f.write('hello\r\n')
f.write('good')
print(f.getvalue())
f.close()
</code> <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>8. Serialization with JSON and pickle
JSON
Use json.dumps() to get a string, json.dump() to write directly to a file, json.loads() to parse a string, and json.load() to read from a file.
<code>import json
names = ['zhangsan', 'lisi', 'wangwu']
result = json.dumps(names)
file = open('names.txt', 'w')
file.write(result)
file.close()
</code>pickle
Pickle serializes Python objects to binary. Use pickle.dumps() or pickle.dump() for serialization, and pickle.loads() or pickle.load() for deserialization.
<code>import pickle
names = ['张三', '李四']
b_names = pickle.dumps(names)
file = open('names.txt', 'wb')
file.write(b_names)
file.close()
</code>JSON is platform‑independent and human‑readable, while pickle is Python‑specific and preserves full object state.
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.