Python3 pathlib添加额外的反斜杠\\

2024-09-24 04:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚了解了pathlib,它应该使文件路径和位置不受操作系统的影响。 然而,当使用write file函数时,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')

它给出以下输出

file to write to is>> subDir\myFile

FileNotFoundError: [Errno 2] No such file or directory: 'subDir\\myFile'


Tags: 文件topath函数路径iswindowsmyfile