由于我同时使用Windows“cmd.exe
和msysgit bash
,因此尝试访问os.getcwd()
的Windows路径输出会导致Python尝试访问以驱动器号和冒号开头的路径,例如C:\
,bash
正确确定了无效的unix路径,而在此示例中,该路径应以/c/
开头。但是,如何将Windows路径修改为msys - 等效iff脚本在bash
内运行?
答案 0 :(得分:0)
丑陋但除非您为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