How do I get stdout directly into a Python variable

Peter Hansen peter at engcorp.com
Mon Feb 25 22:19:15 EST 2002


David Lees wrote:
> 
> I am playing with a Python script that issues system commands on linux
> and I want the stdout to go directly into a Python variable.  Right now
> I am writing to a temp file and then opening it up.  This seems like a
> hack.
> 
> I am doing something like this:
> 
> os.system('diff file1 file2 | wc > temp')
> fh = open('temp','r')
> line = fh.read()
> fh.close()

You're looking for os.popen() and it's ilk, including the
popen2 module.



More information about the Python-list mailing list