【问题标题】:Construct a filepath for a different operating system in Python在 Python 中为不同的操作系统构建文件路径
【发布时间】:2017-08-16 17:18:05
【问题描述】:

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

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

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

【问题讨论】:

  • 当然,Linux 上没有winRoot。在linux上,文件系统的根是"/"
  • @DYZ,抱歉打错了。是的,Linux 上没有 Windows 根;在linux上,文件系统的根是“/”。我们在那里完全同意。然而,在 linux 上,我想构建一个 windows 文件路径。在这个脚本中,winRoot 被硬编码为“c:”

标签: python linux windows filepath


【解决方案1】:

Check this doc page

导入ntpath模块生成windows路径

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

变成

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

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    相关资源
    最近更新 更多