我正在编写一个小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
感谢。
答案 0 :(得分:7)
在Unix上,使用
shell=True
: [...] 如果args是一个序列,则第一项指定命令字符串,任何其他项将被视为附加的参数shell本身。
您不需要shell=True
以及参数列表。设置shell=False
。