Master Python File I/O: Open, Read, Write, Paths, CSV, and Serialization
This comprehensive guide walks you through Python's file handling—from opening and closing files, using absolute and relative paths, and various read/write modes, to copying files, working with CSV, in‑memory streams, and serializing data with JSON and pickle—complete with clear code examples.
File Opening and Closing
The built‑in open() function opens a file and returns a file handle (e.g., f1). You can specify the file path, mode (default 'r' for read), and encoding (default OS encoding; Windows uses GBK, Linux/macOS use UTF‑8). Remember to close the handle with f1.close() or use the with open() context manager, which automatically closes the file.
f1 = open(r'd:\test_file.txt', mode='r', encoding='utf-8')
content = f1.read()
print(content)
f1.close() with open(r'd:\test_file.txt', mode='r', encoding='utf-8') as f1:
content = f1.read()
print(content) open()is a built‑in function that ultimately calls the operating system.
The file handle (e.g., f1, fh, file_handler) is required for all file operations. encoding defaults to the OS setting; you can override it. mode defaults to 'r' (read). f1.close() closes the handle.
Using with open() avoids manual closing and allows handling multiple files in one statement.
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.
Path syntax options: backslashes \, raw string r'\', or forward slashes '/' (recommended).
Common File Access Modes
Text modes (default):
r # read‑only (file must exist)
w # write‑only (creates or truncates)
a # append‑only (creates if missing)Binary modes (use b):
rb # read binary
wb # write binary
ab # append binaryCombined read/write modes use + (e.g., r+, w+, a+) and their binary equivalents ( r+b, w+b, a+b). When using r+, the file pointer starts at the beginning; writing before reading can overwrite existing data.
File Reading and Writing
Reading
Examples with a sample file read.txt containing several lines:
f1 = open('read.txt', encoding='utf-8')
content = f1.read()
print(content, type(content))
f1.close()Reading a specific number of characters: f1.read(6). Reading line by line: f1.readline() or iterating over the file object, which is memory‑efficient.
with open('read.txt', encoding='utf-8') as f1:
for line in f1:
print(line.strip())Writing
Opening a file in write mode creates it or truncates it before writing:
f1 = open('write.txt', mode='w', encoding='utf-8')
f1.write('Lucy is awesome')
f1.close()Binary write mode ( wb) is used for non‑text data such as images.
File Pointer Positioning
tell()returns the current offset; seek(offset, whence) moves the pointer. whence can be 0 (start), 1 (current), or 2 (end).
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()File Copying
import os
src = input('Enter file path:')
if os.path.isfile(src):
with open(src, 'rb') as old_file:
base, ext = os.path.splitext(src)
new_name = base + '.bak' + ext
with open(new_name, 'wb') as new_file:
while True:
chunk = old_file.read(1024)
if not chunk:
break
new_file.write(chunk)
else:
print('File does not exist')CSV Reading and Writing
CSV files store tabular data as plain text, separating fields with commas and rows with newlines.
import csv
# Write
with open('test.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['name', 'age', 'score'])
writer.writerows([
['zhangsan', '18', '98'],
['lisi', '20', '99'],
['wangwu', '17', '90'],
['jerry', '19', '95']
])
# Read
with open('test.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)In‑Memory Streams
Use StringIO for text buffers and BytesIO for binary buffers.
from io import StringIO
buf = StringIO()
buf.write('hello
')
buf.write('good')
print(buf.getvalue())
buf.close() from io import BytesIO
buf = BytesIO()
buf.write('你好
'.encode('utf-8'))
buf.write('中国'.encode('utf-8'))
print(buf.getvalue())
buf.close()Serialization and Deserialization
Python can serialize objects to text (JSON) or binary (pickle) for storage.
JSON Module
json.dumps()returns a JSON string; json.dump() writes directly to a file. json.loads() parses a JSON string; json.load() reads from a file.
import json
names = ['zhangsan', 'lisi', 'wangwu']
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 Module
pickle.dumps()returns binary data; pickle.dump() writes binary to a file. pickle.loads() and pickle.load() perform the reverse.
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:
loaded = pickle.load(f)
print(loaded)JSON vs. pickle
JSON produces human‑readable text and is cross‑platform.
pickle produces binary data, cannot be shared across languages, but preserves Python‑specific objects.
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.
