第五章 · 文件与IO

路径操作

本节目标

学完这一节,你会知道:

  1. 为什么推荐用 pathlib 处理路径
  2. 如何创建 Path 对象
  3. 如何拼接路径、获取文件名和后缀
  4. 如何判断文件或目录是否存在
  5. 如何遍历文件夹里的文件

路径操作经常出现在文件读写、批量整理、数据处理项目里。

先跑一个例子

新建文件 path_demo.py,写入:

from pathlib import Path

folder = Path("data")
file_path = folder / "hello.txt"

folder.mkdir(exist_ok=True)
file_path.write_text("Hello pathlib\n", encoding="utf-8")

print(file_path)
print(file_path.name)
print(file_path.suffix)
print(file_path.read_text(encoding="utf-8"))

运行:

python3 path_demo.py

你会看到当前目录多了一个 data 文件夹,里面有 hello.txt

为什么用 pathlib?

传统路径拼接可能这样写:

import os

path = os.path.join("data", "hello.txt")

pathlib 写法更直观:

from pathlib import Path

path = Path("data") / "hello.txt"

/ 在这里不是除法,而是路径拼接。

创建 Path 对象

from pathlib import Path

current = Path.cwd()
home = Path.home()
relative = Path("data") / "notes.txt"
absolute = Path("/tmp") / "demo.txt"

print(current)
print(home)
print(relative)

相对路径是相对于当前运行目录。

路径信息

file_path = Path("data/report.txt")

print(file_path.name)
print(file_path.stem)
print(file_path.suffix)
print(file_path.parent)

含义:

  • .name:完整文件名
  • .stem:不带后缀的文件名
  • .suffix:后缀
  • .parent:父目录

判断是否存在

path = Path("data/hello.txt")

print(path.exists())
print(path.is_file())
print(path.is_dir())

这些方法在读取文件前很常用。

if path.exists():
    print(path.read_text(encoding="utf-8"))
else:
    print("文件不存在")

读写文本

Path 可以直接读写文本:

path = Path("message.txt")

path.write_text("你好,pathlib", encoding="utf-8")
content = path.read_text(encoding="utf-8")

print(content)

这比 open() 更短,适合简单读写。

如果要逐行处理大文件,仍然可以用 open()path.open()

创建目录和文件

创建目录:

Path("output").mkdir(exist_ok=True)

创建多层目录:

Path("data/2026/notes").mkdir(parents=True, exist_ok=True)

创建空文件:

Path("empty.txt").touch()

删除文件:

Path("empty.txt").unlink(missing_ok=True)

遍历目录

遍历当前目录下的 .py 文件:

for file in Path(".").glob("*.py"):
    print(file)

递归遍历所有 Markdown 文件:

for file in Path(".").rglob("*.md"):
    print(file)

glob() 只看当前层,rglob() 会递归进入子文件夹。

逐行拆解

再看开头的例子:

folder = Path("data")
file_path = folder / "hello.txt"

创建文件夹路径,再拼接出文件路径。

folder.mkdir(exist_ok=True)

如果 data 不存在,就创建。已经存在也不报错。

file_path.write_text("Hello pathlib\n", encoding="utf-8")

把文本写入文件。

自己改一改

path_demo.py 改成:

from pathlib import Path

notes_dir = Path("notes")
notes_dir.mkdir(exist_ok=True)

for i in range(1, 4):
    path = notes_dir / f"note_{i}.txt"
    path.write_text(f"这是第 {i} 篇笔记\n", encoding="utf-8")

for path in notes_dir.glob("*.txt"):
    print(path.name, path.read_text(encoding="utf-8").strip())

然后继续改:

  1. 只输出文件后缀是 .txt 的文件
  2. 统计一共有多少个文件
  3. 创建一个 backup 文件夹

常见错误

1. 忘记导入 Path

path = Path("data.txt")

使用前要写:

from pathlib import Path

2. 父目录不存在

Path("a/b/c").mkdir()

如果 a/b 不存在,会报错。多层目录用:

Path("a/b/c").mkdir(parents=True, exist_ok=True)

3. 把 Path 当字符串随意拼接

不推荐:

path = Path("data") + "file.txt"

推荐:

path = Path("data") / "file.txt"

4. 删除文件前没确认

unlink() 会删除文件。练习时建议只删除自己创建的临时文件。

小练习

练习 1:创建笔记目录

创建 my_notes 文件夹,并在里面创建 day1.txt

练习 2:列出 Python 文件

输出当前目录下所有 .py 文件名。

练习 3:统计 Markdown 文件

递归统计当前目录下有多少个 .md 文件。

参考答案

练习 1:

from pathlib import Path

folder = Path("my_notes")
folder.mkdir(exist_ok=True)

(folder / "day1.txt").write_text("第一天学习记录\n", encoding="utf-8")

练习 2:

from pathlib import Path

for path in Path(".").glob("*.py"):
    print(path.name)

练习 3:

from pathlib import Path

count = 0
for path in Path(".").rglob("*.md"):
    count += 1

print(f"Markdown 文件数量:{count}")

小结

这一节你学会了:

  1. Path() 可以表示文件或目录路径
  2. / 可以拼接路径
  3. .name.stem.suffix.parent 可以获取路径信息
  4. .exists().is_file().is_dir() 可以判断路径状态
  5. glob()rglob() 可以遍历文件

下一节我们会学习JSON 与 CSV。它们是程序和外部系统交换数据时最常见的文件格式。

路径不再靠猜,文件位置开始清楚了

pathlib 会让路径拼接少一点手忙脚乱。把 data、notes、backup 这些文件夹亲手建出来,你会更理解程序到底在操作哪里。马哥建议你多打印 Path.cwd(),先搞清楚自己站在哪个文件夹里。

讨论 (0)

还没有评论,来抢沙发吧!