【问题标题】:How to get the cwd in a shell-dependend format?如何以 shell 依赖格式获取 cwd?
【发布时间】:2016-05-18 08:02:58
【问题描述】:

由于我同时使用 Windows 的 cmd.exebash,因此尝试访问 os.getcwd() 的 Windows 路径输出会导致 Python 尝试访问以驱动器号开头的路径,并且冒号,例如C:\bash 正确地确定了一个无效的 unix 路径,在这个例子中它应该以 /c/ 开头。但是如何修改 Windows 路径以使其成为 -equivalent iff 脚本在 bash 内运行?

【问题讨论】:

标签: msysgit msys python windows bash msys dirname


【解决方案1】:

丑陋但应该可以工作,除非您为 Windows 创建环境变量 SHELL=bash

def msysfy(dirname):
    import os
    try:
        shell = os.environ['SHELL']
    except KeyError:  # by default, cmd.exe has no SHELL variable
        shell = 'win'
    if os.path.basename(shell)=='bash' and dirname[1] == ':':
        return '/' + dirname[0].lower() + '/' + dirname[2:]
        # don't worry about the other backslashes, msys handles them
    else:
        return dirname

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    相关资源
    最近更新 更多