在 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()[0]
请注意删除了 *.sln
周围的双引号并将 shell
设置为 False
。
这确保 *.sln
被逐字传递给 find
并且不会被 shell 扩展。
编辑:以下也适用:
>>> print subprocess.Popen(['find . -iname "*.sln"'],
... stdout=subprocess.PIPE, shell=True).communicate()[0]
关于Python 的 subprocess.Popen() 结果与命令行不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9231074/