1

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.

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it worked. I passed args as string. But still had to set shell=True. otherwise it raises OSError: [Errno 2] No such file or directory
It's safer to pass variable arguments as a list, but if you have a fixed command, there's no harm in sending it through bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.