文件 IO

open() 基础

# 始终用 with,自动关闭文件
with open("data.txt", "r", encoding="utf-8") as f:
    content = f.read()

文件模式

模式说明
r只读(默认),不存在抛 FileNotFoundError
w写入覆盖,不存在则创建
a追加,不存在则创建
x创建新文件,已存在抛 FileExistsError
b二进制模式(组合:rbwb
+读写模式(r+w+

文本读写

# 读取全部
with open("data.txt", encoding="utf-8") as f:
    text = f.read()
 
# 逐行迭代(节省内存)
with open("data.txt", encoding="utf-8") as f:
    for line in f:
        print(line.rstrip("\n"))
 
# 读取所有行
with open("data.txt", encoding="utf-8") as f:
    lines = [l.rstrip() for l in f]
 
# 写入
with open("output.txt", "w", encoding="utf-8") as f:
    f.write("Hello\n")
    f.writelines(["line1\n", "line2\n"])
 
# 追加
with open("log.txt", "a", encoding="utf-8") as f:
    f.write("新日志\n")

二进制读写

with open("image.png", "rb") as f:
    data = f.read()
 
with open("copy.png", "wb") as f:
    f.write(data)
 
# 分块读取大文件
with open("large.bin", "rb") as f:
    while chunk := f.read(4096):
        process(chunk)

pathlib(推荐,3.4+)

from pathlib import Path
 
p = Path("/tmp/data")
 
# 路径拼接
p / "file.txt"           # Path("/tmp/data/file.txt")
 
# 属性
p.parent                 # Path("/tmp")
p.name                   # "data"
p.stem                   # "data"(无扩展名)
p.suffix                 # ""
p.with_suffix(".log")    # Path("/tmp/data.log")
 
# 检查
p.exists()
p.is_file()
p.is_dir()
 
# 创建 / 删除
p.mkdir(parents=True, exist_ok=True)
p.unlink()               # 删除文件
p.rmdir()                # 删除空目录
 
# 直接读写
text = Path("data.txt").read_text(encoding="utf-8")
Path("out.txt").write_text("content", encoding="utf-8")
data = Path("img.png").read_bytes()
 
# 遍历
for f in Path(".").iterdir():       print(f)
for f in Path(".").rglob("*.py"):   print(f)  # 递归
 
Path("file.txt").resolve()          # 绝对路径

JSON

import json
 
data = {"name": "Alice", "scores": [95, 87, 92]}
 
# 写入文件
with open("data.json", "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False, indent=2)
 
# 读取文件
with open("data.json", encoding="utf-8") as f:
    data = json.load(f)
 
# 字符串互转
s = json.dumps(data, ensure_ascii=False)
obj = json.loads(s)

CSV

import csv
 
# 读取
with open("data.csv", encoding="utf-8", newline="") as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row["name"], row["age"])
 
# 写入
fieldnames = ["name", "age"]
rows = [{"name": "Alice", "age": 30}]
 
with open("out.csv", "w", encoding="utf-8", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(rows)

os 模块

import os
 
os.getcwd()
os.makedirs("a/b/c", exist_ok=True)
os.remove("file.txt")
os.rename("old.txt", "new.txt")
os.path.join("/tmp", "a", "b")
os.path.exists("file.txt")
os.path.getsize("file.txt")
os.path.abspath("file.txt")

临时文件

import tempfile
from pathlib import Path
 
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt") as f:
    f.write("temp data")
    print(f.name)
 
with tempfile.TemporaryDirectory() as tmpdir:
    path = Path(tmpdir) / "data.txt"
    path.write_text("hello")

相关链接