Easy way to store stdout to a variable?

Alex Martelli aleax at aleax.it
Mon Mar 10 02:08:59 EST 2003


mike wrote:
   ...
> unix pipes and filters, e.g. "ls *.txt | grep -v old | wc -l".
> 
> I know I can use "os.system()" to invoke system calls, but I don't
   ...
> So what is an easy one-liner way to store stdout of a sytem-call to a
> variable?

    thevar = os.popen('ls *.txt | grep -v old | wc -l').read()


Alex





More information about the Python-list mailing list