当你需要拆分命令行时,比如调用Popen
,最佳实践似乎是:
subprocess.Popen(shlex.split(cmd), ...
但请阅读文档(RTFM)。
shlex
类使编写简单语法的词法分析器变得容易,这些语法类似于Unix shell...
那么,在win32平台上应该怎么做呢?关于引号解析和POSIX模式与非POSIX模式有何区别呢?
当你需要拆分命令行时,比如调用Popen
,最佳实践似乎是:
subprocess.Popen(shlex.split(cmd), ...
但请阅读文档(RTFM)。
shlex
类使编写简单语法的词法分析器变得容易,这些语法类似于Unix shell...
那么,在win32平台上应该怎么做呢?关于引号解析和POSIX模式与非POSIX模式有何区别呢?
到目前为止,Python标准库中还没有有效的适用于Windows/多平台的命令行拆分函数。(2016年3月)
简而言之,对于subprocess.Popen .call
等操作,最好采用以下方式进行:
if sys.platform == 'win32':
args = cmd
else:
args = shlex.split(cmd)
subprocess.Popen(args, ...)
在Windows上,对于shell
选项的任何值,拆分都是不必要的。在内部,Popen只是使用subprocess.list2cmdline
重新组合已拆分的参数 :-)。shell=True
选项,在Unix上也不需要shlex.split
。.bat
或.cmd
脚本(而不是.exe .com),您需要明确包含文件扩展名,除非shell=True
。shlex.split(cmd, posix=0)
在Windows路径中保留反斜杠,但不能正确理解引用和转义。它的posix = 0模式到底有什么用很不清楚,但99%肯定会诱使Windows /跨平台程序员…ctypes.windll.shell32.CommandLineToArgvW
:def win_CommandLineToArgvW(cmd):
import ctypes
nargs = ctypes.c_int()
ctypes.windll.shell32.CommandLineToArgvW.restype = ctypes.POINTER(ctypes.c_wchar_p)
lpargs = ctypes.windll.shell32.CommandLineToArgvW(unicode(cmd), ctypes.byref(nargs))
args = [lpargs[i] for i in range(nargs.value)]
if ctypes.windll.kernel32.LocalFree(lpargs):
raise AssertionError
return args
然而,那个函数CommandLineToArgvW
是虚假的,或者说只是与强制性标准C argv & argc
解析略微相似:
>>> win_CommandLineToArgvW('aaa"bbb""" ccc')
[u'aaa"bbb"""', u'ccc']
>>> win_CommandLineToArgvW('"" aaa"bbb""" ccc')
[u'', u'aaabbb" ccc']
>>>
C:\scratch>python -c "import sys;print(sys.argv)" aaa"bbb""" ccc
['-c', 'aaabbb"', 'ccc']
C:\scratch>python -c "import sys;print(sys.argv)" "" aaa"bbb""" ccc
['-c', '', 'aaabbb"', 'ccc']
请关注Python库中可能会添加的未来内容,详情请见http://bugs.python.org/issue1724822。(“fisheye3”服务器上的所述函数实际上并不正确。)
在 Windows 上,有效的命令行分割是相当疯狂的。例如,请尝试\ \\ \" \\"" \\\"aaa """"
……
我目前提议将以下函数作为 Python 库的候选函数,用于跨平台命令行分割。它是跨平台的;比 shlex 快大约10倍,后者使用单字符步进和流式处理;而且与管道相关的字符也受到尊重(不像 shlex)。它已经通过了 Windows 和 Linux Bash 上的一系列真实困难的 shell 测试以及 test_shlex
的遗留 POSIX 测试模式。欢迎反馈有关剩余错误的信息。
def cmdline_split(s, platform='this'):
"""Multi-platform variant of shlex.split() for command-line splitting.
For use with subprocess, for argv injection etc. Using fast REGEX.
platform: 'this' = auto from current platform;
1 = POSIX;
0 = Windows/CMD
(other values reserved)
"""
if platform == 'this':
platform = (sys.platform != 'win32')
if platform == 1:
RE_CMD_LEX = r'''"((?:\\["\\]|[^"])*)"|'([^']*)'|(\\.)|(&&?|\|\|?|\d?\>|[<])|([^\s'"\\&|<>]+)|(\s+)|(.)'''
elif platform == 0:
RE_CMD_LEX = r'''"((?:""|\\["\\]|[^"])*)"?()|(\\\\(?=\\*")|\\")|(&&?|\|\|?|\d?>|[<])|([^\s"&|<>]+)|(\s+)|(.)'''
else:
raise AssertionError('unkown platform %r' % platform)
args = []
accu = None # collects pieces of one arg
for qs, qss, esc, pipe, word, white, fail in re.findall(RE_CMD_LEX, s):
if word:
pass # most frequent
elif esc:
word = esc[1]
elif white or pipe:
if accu is not None:
args.append(accu)
if pipe:
args.append(pipe)
accu = None
continue
elif fail:
raise ValueError("invalid or incomplete shell string")
elif qs:
word = qs.replace('\\"', '"').replace('\\\\', '\\')
if platform == 0:
word = word.replace('""', '"')
else:
word = qss # may be even empty; must be last
accu = (accu or '') + word
if accu is not None:
args.append(accu)
return args
win_CommandLineToArgvW
看起来是list2cmdline
的确切反向操作。 - Eric