Re: what does "python -i" use as input stream (stdin)?

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

    Re: what does "python -i" use as input stream (stdin)?

    En Fri, 26 Sep 2008 04:29:42 -0300, Almar Klein <almar.klein@gm ail.com>
    escribió:
    I would still like to hear if anyone knows how I can change the input
    stream
    that
    is used when running "python -i", but I would not be surprised if it is
    impossible...
    Sure you can. You have to replace the file descriptor 0, that is,
    "standard input"; sys.stdin reads from there. The standard way is to use
    os.dup2:

    c:\temp>type foo.txt
    This line read from foo.txt


    c:\temp>type redirect.py
    import os

    inp = open("foo.txt", "r")
    os.dup2(inp.fil eno(), 0)
    print "raw_input->", raw_input()

    c:\temp>python redirect.py
    raw_input-This line read from foo.txt

    This is not

    --
    Gabriel Genellina

Working...