Help with 'popen'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stephen_b

    #1

    Help with 'popen'

    Can someone let me know why this won't work? Thanks.
    >>from os import popen
    >>popen('expo rt asdfasdf=hello' ).read()
    ''
    >>popen('echo $asdfasdf').rea d()
    '\n'

    Thanks.

    Stephen

  • James Stroud

    #2
    Re: Help with 'popen'

    stephen_b wrote:
    Can someone let me know why this won't work? Thanks.
    >
    >
    >>>>from os import popen
    >>>>popen('expo rt asdfasdf=hello' ).read()
    >
    ''
    >
    >>>>popen('ec ho $asdfasdf').rea d()
    >
    '\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" .

    pyimport os
    pyos.environ['asdfasdf'] = 'hello'
    pyos.popen('ech o $asdfasdf').rea d()
    'hello\n'

    James

    Comment

    • Tim Roberts

      #3
      Re: Help with 'popen'

      James Stroud <jstroud@mbi.uc la.eduwrote:
      >stephen_b wrote:
      >
      >Can someone let me know why this won't work? Thanks.
      >>
      >>>>>from os import popen
      >>>>>popen('exp ort asdfasdf=hello' ).read()
      >''
      >>>>>popen('ech o $asdfasdf').rea d()
      >'\n'
      >
      >Python starts a new shell for each command, so your environment variable
      >is lost. You probably want this, which makes the variable "permanent" .
      >
      >pyimport os
      >pyos.environ['asdfasdf'] = 'hello'
      >pyos.popen('ec ho $asdfasdf').rea d()
      >'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@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      Working...