Virtual file for subprocess

Discussion in 'Python' started by bobnotbob, Dec 11, 2009.

  1. bobnotbob

    bobnotbob Guest

    I am calling external executable from my python program (using
    subprocess). This external program's output is a text file which I
    then read and parse. Is there any way to "sandbox" the calling of
    this external program so that it writes to a virtual file instead of
    the hardcoded text?
    bobnotbob, Dec 11, 2009
    #1
    1. Advertising

  2. bobnotbob

    Lie Ryan Guest

    On 12/12/2009 4:07 AM, bobnotbob wrote:
    > I am calling external executable from my python program (using
    > subprocess). This external program's output is a text file which I
    > then read and parse. Is there any way to "sandbox" the calling of
    > this external program so that it writes to a virtual file instead of
    > the hardcoded text?


    If the program writes its outputs to the stdout, you can redirect the
    program's stdout using subprocess, try to find a switch that will tell
    it to write to stdout. Otherwise, you're pretty much stuck to using a
    real file AFAIK.
    Lie Ryan, Dec 11, 2009
    #2
    1. Advertising

  3. On 2009-12-11, Lie Ryan <> wrote:
    > On 12/12/2009 4:07 AM, bobnotbob wrote:
    >> I am calling external executable from my python program (using
    >> subprocess). This external program's output is a text file which I
    >> then read and parse. Is there any way to "sandbox" the calling of
    >> this external program so that it writes to a virtual file instead of
    >> the hardcoded text?

    >
    > If the program writes its outputs to the stdout, you can
    > redirect the program's stdout using subprocess, try to find a
    > switch that will tell it to write to stdout. Otherwise, you're
    > pretty much stuck to using a real file AFAIK.


    Most Unix systems have paths that you can pass to programs
    which think they need to write to "files". Accessing those
    files actually access already open file descriptors such as
    stdin, stdout, and stderr.

    On Linux, for example, you can tell the program to write to
    /proc/self/fd/1 and that's actually stdout which can then be a
    pipe connected to the Python program that invoked the program.

    This can be very useful when executing a program which can be
    told what file to write to, but who's author was too
    narrow-minded to provide the option to send output to stdout.

    If you need to get fancy you can create multiple input/output
    pipes that are inherited by the child program and then
    references as /proc/self/fd/<whatever>.

    --
    Grant Edwards grante Yow! These PRESERVES should
    at be FORCE-FED to PENTAGON
    visi.com OFFICIALS!!
    Grant Edwards, Dec 11, 2009
    #3
    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. Derek Simmons
    Replies:
    0
    Views:
    793
    Derek Simmons
    Aug 1, 2004
  2. Jéjé
    Replies:
    2
    Views:
    388
    Jéjé
    Nov 30, 2005
  3. while_1
    Replies:
    2
    Views:
    617
    Roedy Green
    Jun 25, 2004
  4. hiral
    Replies:
    2
    Views:
    584
    Jean-Michel Pichavant
    May 5, 2010
  5. Replies:
    0
    Views:
    450
Loading...

Share This Page