popen child termination

Discussion in 'Python' started by Milos Prudek, Aug 21, 2004.

  1. Milos Prudek

    Milos Prudek Guest

    How can I make popen (popen2, popen3) run and finish inside my program?

    Example:

    def Run(cmd):
    w,r,e = os.popen3(cmd)
    w.close()
    r.close()
    e.close()
    Run('touch ~/xxx')
    os.remove('~/xxx')

    The example above fails with this error message for os.remove: "No such
    file or directory: '~/xxx'"

    But when the os.remove line is deleted, the example correctly creates
    the xxx file.

    I feel that os.wait() deals with this but I do not understand how to use
    it to do what I want.

    --
    Milos Prudek
    Milos Prudek, Aug 21, 2004
    #1
    1. Advertising

  2. Milos Prudek

    Donn Cave Guest

    Quoth Milos Prudek <>:
    | How can I make popen (popen2, popen3) run and finish inside my program?
    |
    | Example:
    |
    | def Run(cmd):
    | w,r,e = os.popen3(cmd)
    | w.close()
    | r.close()
    | e.close()
    | Run('touch ~/xxx')
    | os.remove('~/xxx')
    |
    | The example above fails with this error message for os.remove: "No such
    | file or directory: '~/xxx'"
    |
    | But when the os.remove line is deleted, the example correctly creates
    | the xxx file.
    |
    | I feel that os.wait() deals with this but I do not understand how to use
    | it to do what I want.

    You're right, but you're going to have to back up a little and
    do the popen part a little different.

    popen() itself is simpler, because the wait() is built into the
    pclose(3) function that's called by the close() method. You will
    find that it returns exit status, except when exit status is 0 in
    which case it returns None. (Did that make sense? I hope not.)

    As for popen3(), you will need to instead create the Popen3 instance
    that popen3 creates. Then you can invoke its wait() method. There
    isn't any way to get there from just the file objects that popen3()
    returns.

    Donn Cave,
    Donn Cave, Aug 22, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,068
    David Schwartz
    Dec 9, 2003
  2. Tung Wai Yip
    Replies:
    1
    Views:
    749
    Anders Dalvander
    Oct 30, 2003
  3. Sanjay
    Replies:
    1
    Views:
    642
    George Ter-Saakov
    Feb 21, 2007
  4. Greg Ercolano
    Replies:
    2
    Views:
    2,852
  5. File.popen/IO.popen

    , May 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    208
    Robert Klemme
    May 20, 2006
Loading...

Share This Page