我已经用 python 编写了一段代码,它使用 / 在文件夹中创建一个特定文件,如果我想在 Windows 中使用该代码,它将无法工作,有没有一种方法可以让我在 Windows 和 Linux 中使用该代码。
在 python 中,我正在使用这段代码:
pathfile=os.path.dirname(templateFile)
rootTree.write(''+pathfile+'/output/log.txt')
当我在假设 Windows 机器上使用我的代码时,我的代码将无法工作。
如何在 Linux 和 Windows 中使用“/”(目录分隔符)?
原文由 hulk007 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用
os.path.join()
。示例:os.path.join(pathfile,"output","log.txt")
。在您的代码中是:
rootTree.write(os.path.join(pathfile,"output","log.txt"))