在 Python 中为不同的操作系统构建文件路径

Construct a filepath for a different operating system in Python

我如何使用 python 构建一个路径,用于与脚本 运行 所在的操作系统不同的操作系统?

例如我是 运行 Linux 上的脚本,我想在其中构建 Windows 路径

winPath = os.path.join(winRoot, dir, subdir) #doesn't work when script is run on linux

Check this doc page

导入 ntpath 模块以生成 windows 路径。

winPath = os.join.path(winRoot, dir, subdir)

变成

winPath = ntpath.join(winRoot, dir, subdir)