本文介绍了Python的subprocess.Popen()结果不同于命令行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Windows 7 x32上使用Cygwin,在VS解决方案目录中, find
命令生成正确的结果:
$ find。 -iname* .sln
./ProjName.sln
Python的 subprocess.Popen()
似乎与 *
匹配:
>>> import subprocess
>>>> print subprocess.Popen(['find','。','-iname','* .sln'],
... stdout = subprocess.PIPE,shell = True).communicate 0]
。
./.git
./.git/COMMIT_EDITMSG
./.git/config
./.git/description
< snip>
我的 Popen()
解决方案
以下适用于我:
>>> import subprocess
>>>> print subprocess.Popen(['find','。','-iname','* .sln'],
... stdout = subprocess.PIPE,shell = False).communicate
请注意 *。sln
和 shell
设置为 False
。
这确保 *。sln
被传递给 find
逐字,并且不由shell扩展。 p>
编辑:以下内容也可以使用:
>>>> print subprocess.Popen(['find。-iname* .sln'],
... stdout = subprocess.PIPE,shell = True).communicate()[0]
Using Cygwin on Windows 7 x32, in a VS solution directory, the find
command produces correct results:
$ find . -iname "*.sln"
./ProjName.sln
but the same command with Python's subprocess.Popen()
seems to be matching on *
alone:
>>> import subprocess
>>> print subprocess.Popen(['find', '.', '-iname', '"*.sln"'],
... stdout=subprocess.PIPE, shell=True).communicate()[0]
.
./.git
./.git/COMMIT_EDITMSG
./.git/config
./.git/description
<snip>
What's wrong with myPopen()
call?
解决方案
The following works for me:
>>> import subprocess
>>> print subprocess.Popen(['find', '.', '-iname', '*.sln'],
... stdout=subprocess.PIPE, shell=False).communicate()[0]
Note the removal of double quotes around *.sln
and the setting of shell
to False
.
This makes sure that the *.sln
is passed to find
verbatim and is not expanded by the shell.
edit: The following also works:
>>> print subprocess.Popen(['find . -iname "*.sln"'],
... stdout=subprocess.PIPE, shell=True).communicate()[0]
这篇关于Python的subprocess.Popen()结果不同于命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!