Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python为不同的操作系统构造文件路径_Python_Linux_Windows_Filepath - Fatal编程技术网

用Python为不同的操作系统构造文件路径

用Python为不同的操作系统构造文件路径,python,linux,windows,filepath,Python,Linux,Windows,Filepath,如何使用python构建一个路径,该路径适用于与脚本运行的操作系统不同的操作系统 例如,我正在Linux上运行一个脚本,我想在其中构建Windows路径 winPath = os.path.join(winRoot, dir, subdir) #doesn't work when script is run on linux 导入ntpath模块以生成windows路径 winPath = os.join.path(winRoot, dir, subdir) 变成 winPath = nt

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

例如,我正在Linux上运行一个脚本,我想在其中构建Windows路径

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

导入ntpath模块以生成windows路径

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

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

当然,Linux上没有
winRoot
。在linux上,文件系统的根是
“/”
@DYZ,很抱歉输入错误。是的,Linux上没有windows根目录;在linux上,文件系统的根是“/”。我们在这方面完全一致。然而,在linux上,我想构造一个windows文件路径。在此脚本中,winRoot硬编码为“c:”