Re: How to spawn a program with redirection as it parameter

Discussion in 'Python' started by Donn Cave, Jul 4, 2003.

  1. Donn Cave

    Donn Cave Guest

    Quoth (TH Lim):
    | How do I do a simple thing like /bin/cat myfile.txt >> yourfile.txt
    | in unix? I tried with the script shown below but not successful. Am
    | doing it right? Pls. advise. thank you.
    |
    | #!/usr/bin/python
    | import os
    | os.spawnl(os.P_WAIT, "/bin/cat" , "/bin/cat", "myfile.txt >>
    | yourfile.txt")
    | print "Done"

    No, the '>>' operator belongs to the shell, and in spawnl there
    is no shell. As another followup observes, if you want a shell,
    you can use system() -
    os.system('cat myfile.txt >> yourfile.txt') is about the same as
    os.spawnl(os.P_WAIT, '/bin/sh', 'sh', '-c', 'cat myfile.txt >> yourfile.txt')

    Sometimes we would rather avoid the shell, because there's a risk
    that when it re-evaluates the command parameters they will come
    out different than we intend. In that case, there isn't any standard
    function, you have to write your own. Off the top of my head, maybe
    try something like

    def spawnvio(w, file, cmd, iodx):
    pid = os.fork()
    if pid:
    if w == os.P_WAIT:
    p, s = os.waitpid(pid, 0)
    return s
    else:
    return pid
    else:
    try:
    # Perform redirections
    for n, u in iodx:
    if n != u:
    os.dup2(n, u)
    os.close(n)
    os.execve(file, cmd, os.environ)
    finally:
    os._exit(113)

    yrf = os.open('yourfile.txt', os.O_APPEND|os.O_CREAT)
    spawnvio(os.P_WAIT, '/bin/cat', ['cat', 'myfile.txt'], [(yrf, 1)])
    os.close(yrf)

    Donn Cave,
    Donn Cave, Jul 4, 2003
    #1
    1. Advertising

  2. Donn Cave

    TH Lim Guest

    Thanks Donn, ur explaination sure put things into perspective. before
    this, i was wandering why ">>" works in os.system() and not in
    spawn().well, now i know why.

    "Donn Cave" <> wrote in message news:<1057291400.926913@yasure>...
    > Quoth (TH Lim):
    > | How do I do a simple thing like /bin/cat myfile.txt >> yourfile.txt
    > | in unix? I tried with the script shown below but not successful. Am
    > | doing it right? Pls. advise. thank you.
    > |
    > | #!/usr/bin/python
    > | import os
    > | os.spawnl(os.P_WAIT, "/bin/cat" , "/bin/cat", "myfile.txt >>
    > | yourfile.txt")
    > | print "Done"
    >
    > No, the '>>' operator belongs to the shell, and in spawnl there
    > is no shell. As another followup observes, if you want a shell,
    > you can use system() -
    > os.system('cat myfile.txt >> yourfile.txt') is about the same as
    > os.spawnl(os.P_WAIT, '/bin/sh', 'sh', '-c', 'cat myfile.txt >> yourfile.txt')
    >
    > Sometimes we would rather avoid the shell, because there's a risk
    > that when it re-evaluates the command parameters they will come
    > out different than we intend. In that case, there isn't any standard
    > function, you have to write your own. Off the top of my head, maybe
    > try something like
    >
    > def spawnvio(w, file, cmd, iodx):
    > pid = os.fork()
    > if pid:
    > if w == os.P_WAIT:
    > p, s = os.waitpid(pid, 0)
    > return s
    > else:
    > return pid
    > else:
    > try:
    > # Perform redirections
    > for n, u in iodx:
    > if n != u:
    > os.dup2(n, u)
    > os.close(n)
    > os.execve(file, cmd, os.environ)
    > finally:
    > os._exit(113)
    >
    > yrf = os.open('yourfile.txt', os.O_APPEND|os.O_CREAT)
    > spawnvio(os.P_WAIT, '/bin/cat', ['cat', 'myfile.txt'], [(yrf, 1)])
    > os.close(yrf)
    >
    > Donn Cave,
    TH Lim, Jul 10, 2003
    #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. Ladvánszky Károly

    Question: spawn and redirection

    Ladvánszky Károly, Feb 13, 2004, in forum: Python
    Replies:
    6
    Views:
    663
    Peter Hansen
    Feb 13, 2004
  2. Derek Basch
    Replies:
    2
    Views:
    1,289
    Donn Cave
    Jan 21, 2005
  3. gaurav kashyap
    Replies:
    2
    Views:
    273
    gaurav kashyap
    Oct 16, 2008
  4. Ed Hames
    Replies:
    0
    Views:
    365
    Ed Hames
    Apr 16, 2008
  5. Edgardo Hames
    Replies:
    1
    Views:
    336
    Ed Hames
    May 6, 2008
Loading...

Share This Page