【发布时间】:2020-12-16 16:28:58
【问题描述】:
我正在 Windows 机器上开发一些将在 Linux 环境中运行的代码。我知道 Linux 中文件的路径是我需要写入的 /tmp/。此问题的公认答案不适用于 Windows:Platform-independent file paths?
当我尝试open(path.join('tmp', 'fileToOpen.txt'), 'w') 时,我得到“FileNotFoundError: [Error 2] No such file or directory: 'tmp\fileToWrite.txt'”
如何解决?
【问题讨论】:
-
你最好使用
pathlib而不是os.path -
为此使用
pathlib。另外,tmp不存在于 Windows 上吗?您可以尝试使用tempfile模块。 -
@jakub 它不存在,但我创建了一个 C:\tmp\ 用于测试目的。
标签: python