Python子进程不在Windows上将大括号作为参数传递

时间:2014-01-14 05:07:02

标签: python

在Windows命令行中,以下内容与预期完全相同:

C:\> c:\cygwin\bin\printf "<%s>\n" "{foo}"
{foo}

然而,从本机win32 Python实例内部,大括号被剥离:

>>> subprocess.Popen(['c:\\cygwin\\bin\\printf', '<%s>\n', '{foo}'],
...                  stdout=subprocess.PIPE).communicate()[0].rstrip('\n')
'foo'

这里发生了什么?

1 个答案:

答案 0 :(得分:2)

您应该可以使用pipes.quote()来解决此问题: http://docs.python.org/2/library/pipes.html#pipes.quote

或者,如果您使用的是python3,请结帐shlex.quotes():http://docs.python.org/2/library/shlex.html#module-shlex