Get the Output of System Command as a String in Python

Use os.popen():

output = os.popen('ls').read()

The newer way (> Python 2.6) to do this is to use subprocess:

output = subprocess.Popen('ls', stdout=subprocess.PIPE).stdout.read()