当在msys2-Python(3.4.3)中时,我怎么能从msys文件系统获取某些文件的本机Windows路径?
我想为本机Windows应用程序编写配置文件,因此不考虑msys路径重写。
有一个解决方案,但我不喜欢它,因为我必须将未知路径提供给shell:
path = "/home/user/workspace"
output = subprocess.check_output( 'cmd //c echo ' + path, shell = True )
返回C:/msys64/home/user/workspace
。
这个问题并不是Convert POSIX->WIN path, in Cygwin Python, w/o calling cygpath的重复,因为它与 msys2 有关。
答案 0 :(得分:1)
运行cygpath -w NAME
。 cygpath
是/usr/bin
中的命令行实用程序,您可以像运行任何其他命令行实用程序一样运行。输出将是与您传递的NAME参数对应的Windows样式路径。
答案 1 :(得分:0)
根据这个伟大的答案https://stackoverflow.com/a/38471976/5892524,这里重写了相同的代码,以便在msys2中使用。
import ctypes
import sys
xunicode = str if sys.version_info[0] > 2 else eval("unicode")
# If running under Msys2 Python, just use DLL name
# If running under non-Msys2 Windows Python, use full path to msys-2.0.dll
# Note Python and msys-2.0.dll must match bitness (i.e. 32-bit Python must
# use 32-bit msys-2.0.dll, 64-bit Python must use 64-bit msys-2.0.dll.)
msys = ctypes.cdll.LoadLibrary("msys-2.0.dll")
msys_create_path = msys.cygwin_create_path
msys_create_path.restype = ctypes.c_void_p
msys_create_path.argtypes = [ctypes.c_int32, ctypes.c_void_p]
# Initialise the msys DLL. This step should only be done if using
# non-msys Python. If you are using msys Python don't do this because
# it has already been done for you.
if sys.platform != "msys":
msys_dll_init = msys.msys_dll_init
msys_dll_init.restype = None
msys_dll_init.argtypes = []
msys_dll_init()
free = msys.free
free.restype = None
free.argtypes = [ctypes.c_void_p]
CCP_POSIX_TO_WIN_A = 0
CCP_POSIX_TO_WIN_W = 1
CCP_WIN_A_TO_POSIX = 2
CCP_WIN_W_TO_POSIX = 3
def win2posix(path):
"""Convert a Windows path to a msys path"""
result = msys_create_path(CCP_WIN_W_TO_POSIX, xunicode(path))
if result is None:
raise Exception("msys_create_path failed")
value = ctypes.cast(result, ctypes.c_char_p).value
free(result)
return value
def posix2win(path):
"""Convert a msys path to a Windows path"""
result = msys_create_path(CCP_POSIX_TO_WIN_W, str(path))
if result is None:
raise Exception("msys_create_path failed")
value = ctypes.cast(result, ctypes.c_wchar_p).value
free(result)
return value