Q:

How to redirect system call output to a string

Possible Duplicate:
Pipe subprocess standard output to a variable 

I am running a python program :
import os
os.system("ls") # ls command runs on the terminal 

To store the output in a file : 
os.system("ls > a.txt")

What I need is, it stores the output in some temporary string .  IS THAT POSSIBLE ?? 

A:

import subprocess
output = subprocess. Popen(["ls"], stdout = subprocess. PIPE, stderr = subprocess. STDOUT).communicate()[0]

Here you run the external command ls and redirects both the stderr and stdout strewams of the command to the variable output. 
Where the streams have to be redirected are specified using the arguments stdout and stderr of the Popen function. 