开发者

Running git commands using subprocess.Popen in python

开发者 https://www.devze.com 2023-03-16 18:33 出处:网络
I am writing a small python script that needs to execute git commands from inside a given directory The code is as follows:

I am writing a small python script that needs to execute git commands from inside a given directory

The code is as follows:

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

But it shows git usage as the out开发者_开发知识库put.

If the command doesn't involve git for eg. ['ls'] then it shows the correct output.

Is there anything I am missing ?

python version - 2.6.6

Thanks.


subprocess.Popen:

On Unix, with shell=True: […] If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.

You don't want shell=True and also a list of arguments. Set shell=False.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号