在python中使用subprocess.Popen运行git命令

时间:2011-06-30 17:30:47

标签: python git popen

我正在编写一个小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

感谢。

1 个答案:

答案 0 :(得分:7)

subprocess.Popen

  

在Unix上,使用shell=True [...] 如果args是一个序列,则第一项指定命令字符串,任何其他项将被视为附加的参数shell本身

您不需要shell=True以及参数列表。设置shell=False