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

问题描述 投票:1回答:1

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

谢谢。

python git popen
1个回答
7
投票

subprocess.Popen

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

你不想要shell=True以及一个参数列表。设置shell=False

© www.soinside.com 2019 - 2024. All rights reserved.