1

I am trying to save the output of

git remote show origin 

with

  tempf = open('.tmp', 'w+')
  tempf2 = open('.tmp2', 'w+')
  subprocess.Popen(["git", "remote", "show", "origin"], stdout=tempf, stderr=tempf2)
  tempf.close()
  tempf2.close()
  output = open('.tmp', 'r')
  gitoutput = output.read()

and later parse the output with a regex.

However, the code above keeps returning None for gitoutput.

Is there something that I am missing? I am fairly confused as applying .seek(0) does not change the output and running cat .tmp shows the correct contents.

EDIT: stderr is also captured (stderr=tempf2) and thrown away as the git server produces unwanted output to the command line when running the script.

1
  • You can try subprocess.check_call instead Commented Nov 5, 2014 at 0:16

2 Answers 2

1

Use .wait() with Popen

import subprocess
with open('.tmp', 'w+') as tempf,  open('.tmp2', 'w+') as tempf2:
    proc  = subprocess.Popen(["git", "remote", "show", "origin"], stdout=tempf, stderr=tempf2)
    proc.wait()
    tempf.seek(0)
    gitoutput = tempf.read()
print(gitoutput)

Or just use check_call:

with open('.tmp', 'w+') as tempf,  open('.tmp2', 'w+') as tempf2:
    proc  = subprocess.check_call(["git", "remote", "show", "origin"], stdout=tempf, stderr=tempf2)
    tempf.seek(0)
    gitoutput = tempf.read()
print(gitoutput)
Sign up to request clarification or add additional context in comments.

Comments

0

try like this

import subprocess
child = subprocess.Popen(["git", "remote", "show", "origin"], stdout=subprocess.PIPE,shell=True)
msg,err = child.communicate()
print msg
print err

here communicate will return a tuple containing the output and error message that is (stdoutdata,stderrdata)

Comments

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.