Help with 'popen'

Tim Roberts timr at probo.com
Tue Jan 23 08:30:44 CET 2007


James Stroud <jstroud at mbi.ucla.edu> wrote:
>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'
>
>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'

For completeness, let us anticipate the followup question and point out
that "permanent" here means "for this process and any processes that it
spawns".  Once the Python session ends, the "asdfasdf" will be lost.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list