Fundamentals 19 min read

Comprehensive Guide to Python File Operations: Opening, Reading, Writing, Paths, CSV, JSON, and Pickle

This article provides a thorough tutorial on Python file handling, covering how to open and close files, read and write data, work with absolute and relative paths, use different file modes, copy files, manipulate CSV files, and serialize data with JSON and pickle modules.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Comprehensive Guide to Python File Operations: Opening, Reading, Writing, Paths, CSV, JSON, and Pickle

Comprehensive Guide to Python File Operations

This tutorial introduces the essential techniques for handling files in Python, organized into seven sections that cover opening/closing files, reading/writing data, path handling, file modes, copying files, CSV I/O, and data serialization.

1. Opening and Closing Files

The built‑in open() function opens a file and returns a file handle (e.g., f1 ). By default it uses the operating system's encoding (Windows: GBK, Linux/macOS: UTF‑8) and mode 'r' . Always close the handle with f1.close() or use a with statement to close automatically.

<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>

Using with avoids manual closing and allows handling multiple files in a single statement.

Absolute and Relative Paths

Absolute path: full location from the root, 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: Windows style: file = open('C:\\Users\\Python基础\\xxx.txt') Raw string: file = open(r'C:\Users\Python基础\xxx.txt') POSIX style (recommended): file = open('C:/Users/Python基础/xxx.txt')

2. Common File Access Modes

r – read‑only (default, file must exist).

w – write‑only (creates file or truncates existing).

a – append‑only (creates file or appends to existing).

Binary modes: rb , wb , ab for non‑text data.

Combined modes: r+ , w+ , a+ for read/write.

3. Reading and Writing Data

Reading

read() – reads the whole file (or read(n) for n characters/bytes).

readline() – reads a single line.

readlines() – returns a list of all lines (caution with large files).

Iterating over the file object yields one line at a time, which is memory‑efficient.

<code>f1 = open('文件操作的读', encoding='utf-8')
content = f1.read(6)
print(content)  # lucy最帅
f1.close()</code>
<code>f1 = open('文件操作的读', encoding='utf-8')
for line in f1:
    print(line.strip())
f1.close()</code>

Writing

w mode creates a new file or truncates an existing one before writing.

wb writes binary data (e.g., images).

Closing the handle is required before reopening in w to actually clear the file.

<code>f1 = open('文件操作的写', encoding='utf-8', mode='w')
f1.write('lucy真帅')
f1.close()</code>

4. File 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>

5. Implementing a File Copy Function

<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)
        if not content:
            break
        new_file.write(content)
    new_file.close()
    old_file.close()
else:
    print('您输入的文件不存在')</code>

6. CSV File Reading and Writing

CSV (Comma‑Separated Values) stores tabular data as plain text. Use the csv module for convenient I/O.

<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 & BytesIO)

io.StringIO provides a file‑like object for text data; io.BytesIO does the same for binary data.

<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. Using the sys Module for I/O Redirection

Redirect standard streams to files.

<code>import sys
m = open('stdout.txt', 'w', encoding='utf8')
sys.stdout = m
print('hello')
print('yes')
print('good')
m.close()</code>
<code>import sys
x = open('stderr.txt', 'w', encoding='utf8')
sys.stderr = x
print(1/0)
x.close()</code>

9. Serialization and Deserialization

Python objects can be persisted using json (text) or pickle (binary).

JSON

<code>import json
names = ['zhangsan', 'lisi', 'wangwu', 'jerry']
result = json.dumps(names)
with open('names.txt', 'w') as f:
    f.write(result)
# or directly
with open('names.txt', 'w') as f:
    json.dump(names, f)
# loading
with open('names.txt', 'r') as f:
    data = json.load(f)
print(data)</code>

Pickle

<code>import pickle
names = ['张三', '李四', '杰克', '亨利']
b_names = pickle.dumps(names)
with open('names.txt', 'wb') as f:
    f.write(b_names)
# or dump directly
with open('names.txt', 'wb') as f:
    pickle.dump(names, f)
# loading
with open('names.txt', 'rb') as f:
    data = pickle.load(f)
print(data)</code>

JSON produces portable, human‑readable text, while pickle stores exact Python objects in binary form but is not cross‑platform.

Conclusion

The article equips readers with practical code snippets and explanations for all common file‑related tasks in Python, from basic I/O to advanced serialization, enabling efficient data handling in everyday programming.

PythonSerializationJSONCSVPicklefile-iopathlib
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.