Fundamentals 6 min read

Python Standard Library: File System and OS Modules Overview

This article provides a concise tutorial on Python's standard library modules for interacting with the operating system and handling files, covering os, os.path, shutil, glob, pathlib, io, tempfile, fileinput, pickle, csv, zipfile, and tarfile with example code snippets.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Python Standard Library: File System and OS Modules Overview

1. os module – Offers functions to interact with the operating system, such as retrieving the current working directory and listing directory contents.

import os
current_dir = os.getcwd()
print("当前工作目录:", current_dir)
files = os.listdir('.')
print("当前目录下的文件和文件夹:", files)

2. os.path submodule – Provides utilities for pathname manipulation, including joining path components and checking path existence.

import os
path = os.path.join('用户', '文档', '报告.docx')
print("组合后的路径:", path)
exists = os.path.exists('report.docx')
print("路径存在吗:", exists)

3. shutil module – Supplies high‑level file operations such as copying and moving files or directories.

import shutil
shutil.copy('source.txt', 'destination.txt')
print("文件已复制")
shutil.move('old_location/file.txt', 'new_location/file.txt')
print("文件已移动")

4. glob module – Finds pathnames matching a pattern using Unix shell‑style wildcards.

import glob
txt_files = glob.glob('*.txt')
print("找到的文本文件:", txt_files)

5. pathlib module – Introduced in Python 3.4, it offers an object‑oriented approach to filesystem paths, allowing iteration over directory entries and pattern‑based searches.

from pathlib import Path
p = Path('.')
for file in p.iterdir():
    if file.is_file():
        print(file)
for txt_file in p.glob('*.txt'):
    print(txt_file)

6. io module – Contains classes for handling various I/O operations, including reading from and writing to text files.

import io
with open('example.txt', 'r', encoding='utf-8') as f:
    content = f.read()
    print(content)
with open('output.txt', 'w', encoding='utf-8') as f:
    f.write("这是一个测试。\n")
    print("数据已写入文件")

7. tempfile module – Provides utilities for creating temporary files and directories, useful for short‑lived data storage.

import tempfile
with tempfile.TemporaryFile() as temp:
    temp.write(b'Hello World!')  # 写入字节数据
    temp.seek(0)  # 回到文件开头
    print(temp.read())  # 读取数据

8. fileinput module – Allows iteration over lines from one or more input files, simplifying command‑line file processing.

import fileinput
for line in fileinput.input(['file1.txt', 'file2.txt']):
    print(line, end='')  # 默认会添加换行符,所以使用end=''来避免重复换行

9. pickle module – Implements object serialization (pickling) and deserialization (unpickling) for persisting complex data structures.

import pickle
data = {'key': 'value'}
# 序列化
with open('data.pkl', 'wb') as f:
    pickle.dump(data, f)
# 反序列化
with open('data.pkl', 'rb') as f:
    loaded_data = pickle.load(f)
    print("加载的数据:", loaded_data)

10. csv module – Provides functionality to read from and write to CSV (comma‑separated values) files.

import csv
with open('example.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)
with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['Name', 'Age'])
    writer.writerow(['Alice', 30])

11. zipfile module – Enables creation, reading, and extraction of ZIP archives.

import zipfile
with zipfile.ZipFile('example.zip', 'r') as zip_ref:
    zip_ref.extractall('destination_folder')
    print("文件已解压")

12. tarfile module – Offers full support for TAR archive files, including gzip‑compressed variants.

import tarfile
with tarfile.open('example.tar.gz', 'r:gz') as tar:
    tar.list()
PythonFile I/OOSModulesstandard-library
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

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.