Search code examples
pythongitpopen

Running git commands using subprocess.Popen in python


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 output.

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.


Solution

  • 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.