如何以 shell-dependend 格式获取 cwd?
How to get the cwd in a shell-dependend format?
因为我同时使用 Windows' cmd.exe
和 msysgit's bash
, trying to access the Windows-path output by os.getcwd()
is causing Python to attempt accessing a path starting with a drive letter and a colon, e.g. C:\
, which bash
correctly determines an invalid unix-path, which instead should start with /c/
in this example. But how can I modify a Windows-path to become its msys-equivalent iff 脚本是 运行 在 bash
?
丑陋但应该可以工作,除非您为 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
因为我同时使用 Windows' cmd.exe
和 msysgit's bash
, trying to access the Windows-path output by os.getcwd()
is causing Python to attempt accessing a path starting with a drive letter and a colon, e.g. C:\
, which bash
correctly determines an invalid unix-path, which instead should start with /c/
in this example. But how can I modify a Windows-path to become its msys-equivalent iff 脚本是 运行 在 bash
?
丑陋但应该可以工作,除非您为 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