打开文件
使用 open(filename, mode)
打开文件或创建新文件
使用 close()
关闭文件
访问模式
- r:只读,指针在文件开头
- w:只写,会覆盖原文件
- a:追加,指针在文件尾部
- rb,wb,ab:以二进制操作
- r+,w+,a+:;支持读写,特点与对应模式一致
- rb+,wb+,ab+
读写文件
-
write(str)
:写入文件,返回写入的字符数
-
read(n)
:读取 n 个字符,同时指针后移
-
readline()
:读取一行,同时;指针移动到下一行
-
readlines()
:读取所有行,返回一个列表,每个元素为一行
-
tell()
:返回文件指针目前位置
-
seek(offset, from_what)
:移动文件指针
offset 为移动字符数,从尾部开始移动,offset 为负数
from_what 可选 0(文件开头),1(当前位置),2(文件末尾)
pathlib 模块
pathlib
是 Python 3.4+ 标准库中引入的一个模块,提供了一种面向对象的方式来处理文件系统路径,自动处理不同操作系统中的路径,可以完全替代 os.path
模块
基本用法
使用 Path
对象表示一个路径
1 2 3 4 5 6 7 8 9 10
| current_dir = Path.cwd() print(f"当前目录: {current_dir}")
home_dir = Path.home() print(f"用户主目录: {home_dir}")
file_path = Path("data/sample.txt")
|
路径操作
Path
对象重载了 /
运算符,可以实现路径的拼接操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| config_path = home_dir / "config" / "settings.ini" print(f"配置文件路径: {config_path}")
path = Path("/Users/username/Documents/report.pdf") print(f"根目录: {path.root}") print(f"父目录: {path.parent}") print(f"文件名: {path.name}") print(f"stem: {path.stem}") print(f"后缀: {path.suffix}") print(f"所有后缀: {path.suffixes}")
resolved_path = path.resolve() print(f"绝对路径: {resolved_path}")
|
判断路径是否存在以及是否是文件或目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| path = Path("example.txt")
if path.exists(): print(f"{path} 存在") else: print(f"{path} 不存在")
if path.is_file(): print(f"{path} 是文件")
if path.is_dir(): print(f"{path} 是目录")
|
文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| text_file = Path("example.txt") text_file.write_text("Hello, pathlib!")
content = text_file.read_text() print(content)
binary_file = Path("data.bin") binary_file.write_bytes(b'\x00\x01\x02\x03') data = binary_file.read_bytes()
old_file = Path("old_name.txt") new_file = Path("new_name.txt") if old_file.exists(): old_file.rename(new_file)
if new_file.exists(): new_file.unlink()
|
目录操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| new_dir = Path("new_directory") new_dir.mkdir(exist_ok=True)
multi_dir = Path("level1/level2/level3") multi_dir.mkdir(parents=True, exist_ok=True)
current_dir = Path.cwd() print("目录内容:") for item in current_dir.iterdir(): print(f" - {item.name}")
print("\n递归遍历目录:") for item in current_dir.rglob("*"): print(f" - {item.relative_to(current_dir)}")
empty_dir = Path("empty_directory") if empty_dir.exists() and not any(empty_dir.iterdir()): empty_dir.rmdir()
import shutil non_empty_dir = Path("non_empty_directory") if non_empty_dir.exists(): shutil.rmtree(non_empty_dir)
|