首先放上原文链接:读写文本文件 · explore-python
一、读写文本文件
1 读文件
2 读取所有内容
3 按字节读取
4 逐行读取
5 文件迭代器
6 写文件
二、读写二进制文件
Python 不仅支持文本文件的读写,也支持二进制文件的读写,比如图片,声音文件等。
1 读取二进制文件
2 写入二进制文件
三、os模块
官方文档:os.path — Common pathname manipulations — Python 3.12.3 documentation