3

Possible Duplicate:
Running shell command from python and capturing the output

When I want to capture shell execution output, I do this.

declare TAGNAME=`git describe --tags` 

Simple. I looked for this in Python, but most of them looks very complex. What's the simplest way to do this? Yeah I know I can make a function, but I want to know pre-defined function if it is exist.

tagname = theFunc('git describe --tags')
Community
  • 1
  • 1
eonil
  • 83,476
  • 81
  • 317
  • 516

2 Answers2

6

Try:

>>> import subprocess
>>> tagname = subprocess.check_output('git describe --tags'.split())
Konrad Hałas
  • 4,844
  • 3
  • 18
  • 18
1

You should take a look at the subprocess module. You can capture the output of a command with it as well. There are many examples in the manual page how the module is used.

Example:

output=subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE,
    stderr=subprocess.PIPE).communicate()

The result is a tuple with stdout and stderr that was captured from the command.

hochl
  • 12,524
  • 10
  • 53
  • 87