我正在编写一个小python脚本,需要从给定目录中执行git命令
代码如下:
import subprocess, os
pr = subprocess.Popen(['/usr/bin/git', 'status'],
cwd=os.path.dirname('/path/to/dir/'),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
(out, error) = pr.communicate()
print out
但它显示git用法作为输出。
如果该命令不涉及例如git。 ['ls']
然后它显示正确的输出。
有什么我想念的吗?
python版本 - 2.6.6
谢谢。
在Unix上,使用
shell=True
:[...]如果args是一个序列,则第一个项指定命令字符串,任何其他项将被视为shell本身的附加参数。
你不想要shell=True
以及一个参数列表。设置shell=False
。