Comprehensive Guide to Python File Operations: Opening, Reading, Writing, Paths, Modes, CSV, and Serialization
This tutorial presents a complete overview of Python file handling, covering opening and closing files, absolute and relative paths, various access modes, reading and writing techniques, pointer control, file copying, CSV processing, in‑memory streams, sys redirection, and JSON/pickle serialization, all illustrated with practical code examples.
Python provides versatile file operation functions that are essential for everyday programming. This guide consolidates knowledge on opening and closing files, using open() and the with statement, and explains default encoding and mode parameters.
Opening and closing files
<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>The file handle (e.g., f1 ) is used for all subsequent operations. If encoding is omitted, the OS default is used (Windows GBK, Linux/macOS UTF‑8). The default mode is 'r' .
Advantages of with open()
<code># No need to manually close the file
with open('file_read.txt', encoding='utf-8') as f1:
print(f1.read())
# Multiple file handles in one statement
with open('file_read.txt', encoding='utf-8') as f1, \
open('file_write.txt', encoding='utf-8', mode='w') as f2:
print(f1.read())
f2.write('hahaha')</code>Absolute and relative paths
Absolute path: full location, e.g., C:/Users/chris/AppData/Local/Programs/Python/Python37/python.exe
Relative path: starts from the current directory, e.g., test.txt , ./test.txt , ../test.txt , demo/test.txt
Three common ways to write a Windows path:
\file = open('C:\Users\chris\Desktop\Python基础\xxx.txt')
r'file = open(r'C:\Users\chris\Desktop\Python基础\xxx.txt')
'/' (recommended) file = open('C:/Users/chris/Desktop/Python基础/xxx.txt')
Common file access modes
<code>r # read‑only (default, file must exist)
w # write‑only (creates or truncates file)
a # append‑only (creates if missing)
</code>Binary modes add a b suffix:
<code>rb # read binary
wb # write binary
ab # append binary
</code>Combined modes with + allow both reading and writing:
<code>r+ # read/write, start at beginning
w+ # write/read, truncates then reads
a+ # append/read, start at end
</code>Reading files
read() – reads the whole file (or read(n) reads *n* characters/bytes).
readline() – reads a single line.
readlines() – returns a list of all lines (may consume much memory).
Iterating over the file object yields one line at a time, which is memory‑efficient.
<code># Example of read()
f1 = open('file_read.txt', encoding='utf-8')
content = f1.read()
print(content, type(content))
f1.close()
</code> <code># Example of iteration
f1 = open('file_read.txt', encoding='utf-8')
for line in f1:
print(line.strip())
f1.close()
</code>Writing files
<code># Write text (creates or truncates)
f1 = open('file_write.txt', encoding='utf-8', mode='w')
f1.write('lucy真帅')
f1.close()
</code>Binary writing uses wb :
<code># Copy an image in binary mode
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>Pointer positioning
<code>f = open('test.txt')
print(f.read(10)) # read first 10 characters
print(f.tell()) # current offset
f.seek(2, 0) # move to byte 2 from start
print(f.read())
f.seek(1, 1) # move 1 byte forward from current
print(f.read())
f.seek(-4, 2) # move 4 bytes back from end
print(f.read())
f.close()
</code>File copy example
<code>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:
content = old_file.read(1024)
new_file.write(content)
if not content:
break
new_file.close()
old_file.close()
else:
print('您输入的文件不存在')
</code>CSV read/write
<code># Write CSV
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># Read CSV
import csv
file = open('test.csv', 'r')
reader = csv.reader(file)
for row in reader:
print(row)
file.close()
</code>In‑memory streams
<code># StringIO (text)
from io import StringIO
f = StringIO()
f.write('hello\r\n')
f.write('good')
print(f.getvalue())
f.close()
</code> <code># BytesIO (binary)
from io import BytesIO
f = BytesIO()
f.write('你好\r\n'.encode('utf-8'))
f.write('中国'.encode('utf-8'))
print(f.getvalue())
f.close()
</code>Redirecting standard streams with sys
<code># Redirect stdout to a file
import sys
m = open('stdout.txt', 'w', encoding='utf8')
sys.stdout = m
print('hello')
print('yes')
print('good')
m.close()
</code> <code># Redirect stderr to a file (example of an exception)
import sys
x = open('stderr.txt', 'w', encoding='utf8')
sys.stderr = x
print(1/0) # raises ZeroDivisionError
x.close()
</code>Serialization with JSON
<code># dumps returns a JSON string
import json
names = ['zhangsan', 'lisi', 'wangwu', 'jerry', 'henry', 'merry', 'chris']
result = json.dumps(names)
print(type(result)) # <class 'str'>
file = open('names.txt', 'w')
file.write(result)
file.close()
</code> <code># dump writes directly to a file
import json
file = open('names.txt', 'w')
json.dump(names, file)
file.close()
</code> <code># loads converts a JSON string back to a Python object
import json
result = json.loads('["zhangsan", "lisi", "wangwu", "jerry", "henry", "merry", "chris"]')
print(type(result)) # <class 'list'>
</code> <code># load reads from a file object
import json
file = open('names.txt', 'r')
result = json.load(file)
print(result)
file.close()
</code>Serialization with pickle
<code># dumps returns binary data
import pickle
names = ['张三', '李四', '杰克', '亨利']
b_names = pickle.dumps(names)
file = open('names.txt', 'wb')
file.write(b_names)
file.close()
</code> <code># dump writes binary directly to a file
import pickle
file2 = open('names.txt', 'wb')
pickle.dump(names, file2)
file2.close()
</code> <code># loads converts binary back to Python object
import pickle
file1 = open('names.txt', 'rb')
x = file1.read()
y = pickle.loads(x)
print(y)
file1.close()
</code> <code># load reads from a binary file
import pickle
file3 = open('names.txt', 'rb')
z = pickle.load(file3)
print(z)
</code>JSON vs. pickle
JSON produces a human‑readable string and is cross‑platform; only basic data types are supported.
Pickle produces binary data, preserves full Python object state, but is Python‑specific and not safe for untrusted sources.
Both modules provide dump/dumps for serialization and load/loads for deserialization.
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.