Help with 'popen'

James Stroud jstroud at mbi.ucla.edu
Mon Jan 22 00:59:08 EST 2007


stephen_b wrote:
> Can someone let me know why this won't work? Thanks.
> 
> 
>>>>from os import popen
>>>>popen('export asdfasdf=hello').read()
> 
> ''
> 
>>>>popen('echo $asdfasdf').read()
> 
> '\n'
> 
> Thanks.
> 
> Stephen
> 

Python starts a new shell for each command, so your environment variable 
is lost. You probably want this, which makes the variable "permanent".

py> import os
py> os.environ['asdfasdf'] = 'hello'
py> os.popen('echo $asdfasdf').read()
'hello\n'

James



More information about the Python-list mailing list