Run command with no terminal output

Posted by Insomaniacal on Stack Overflow See other posts from Stack Overflow or by Insomaniacal
Published on 2010-12-22T23:45:47Z Indexed on 2010/12/22 23:54 UTC
Read the original article Hit count: 134

Filed under:
|
|
|

Hello, I've searched around online, but can't find the answer to my question. What I want to do is run a command in pythong, using the subprocess module, and store the output in a variable. However, I do not want the command's output to be printed to the terminal. For this code:

def storels():
   a = subprocess.Popen("ls",shell=True)
storels()

I get the directory listing in the terminal, instead of having it stored in a. I've also tried

 def storels():
       subprocess.Popen("ls > tmp",shell=True)
       a = open("./tmp")
       [Rest of Code]
 storels()

This also prints the output of ls to my terminal. I've even tried this command with the somewhat dated os.system method, since running "ls > tmp" in the terminal doesn't print ls to the terminal at all, but stores it in tmp. However, the same thing happens. I'm using python 2.6.

Any suggestions? Thanks in advance!

© Stack Overflow or respective owner

Related posts about python

Related posts about output