Python 文件操作:open 模式、with、os 与 pathlib
open(path, mode):r 读/w 写/a 追加/r+ 读写;b 二进制(rb/wb);t 文本(默认);encoding='utf-8' 指定编码(写文件必带)。with open(...) as f 自动关闭。常用方法:read/readline/readlines、write、seek/tell(指针)、flush。os 模块:os.path.join/exists/makedirs/remove/rename、os.listdir、os.walk 遍历。pathlib(3.4+ 推荐):Path('a/b'),p.exists()/p.mkdir(parents=True)/p.read_text()/p.write_text()/p.glob('*.txt')/p.iterdir()——面向对象、跨平台。
一起交流
分享经验,让问题更进一步
讨论,从你的观点开始
还没有回复,欢迎补充经验或分享不同的思路。