我刚刚了解了 pathlib,据说它应该使文件路径和位置与操作系统无关。但是,当使用写入文件功能时,pathlib 在 windows "\" 中添加了一个额外的反斜杠(尚未在基于 POSIX 的操作系统中测试),这里是代码
from pathlib import Path
path = Path("subDir/myFile")
print("file to write to is>>", path)
with path.open(mode='a') as outputlog:
outputlog.write('foo')
它给出以下输出
要写入的文件是>> subDir\myFile
FileNotFoundError:[Errno 2] 没有这样的文件或目录:'subDir\\myFile'