f



redirecting Stdin

Hi
Is is Possible to redirect the Python stdin in order to receive stdin
commands from a file ?
The time of redirection can  last until exiting the Python  and should
be transparent for python it self.

with regards 
Jon




0
12/3/2003 9:52:14 AM
comp.lang.python 77058 articles. 4 followers. Post Follow

5 Replies
2401 Views

Similar Articles

[PageSpeed] 37

Jon Arter wrote:

> Is is Possible to redirect the Python stdin in order to receive stdin
> commands from a file ?
> The time of redirection can  last until exiting the Python  and should
> be transparent for python it self.

Sure:

	sys.stdin = file('newinput')

-- 
   Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ 
\__/ The map is not the territory.
    -- Alfred Korzybski
0
max78 (1220)
12/3/2003 9:54:11 AM
assume  we have a file :
after redirection   sys.stdin = file('newinput')

-----------------------------------
#newinput file 

import socket

def detectip():
    buf = socket.gethostname()
    remotehost = socket.gethostbyname(buf)
    return remotehost


if __name__ == "__main__":

    print "Your IP address is", detectip()
    raw_input()
------------------------

using 

line=sys.stdin:readline()
exec line

This results in a parsing error when the file reading reaches the
 def detectip(): 
sentence . 
You can execute the command line as long as it is a single line
command 

How to Get arround this ?

With regards 
JON


On Wed, 03 Dec 2003 01:54:11 -0800, Erik Max Francis <max@alcyone.com>
wrote:

>Jon Arter wrote:
>
>> Is is Possible to redirect the Python stdin in order to receive stdin
>> commands from a file ?
>> The time of redirection can  last until exiting the Python  and should
>> be transparent for python it self.
>
>Sure:
>
>	sys.stdin = file('newinput')

0
12/3/2003 10:45:15 AM
Jon Arter wrote:

> after redirection   sys.stdin = file('newinput')

This isn't after redirection, it causes it.

> line=sys.stdin:readline()
> exec line

This contains at least two errors:  You meant a period instead of a
colon, and the exec statement makes no sense.

> This results in a parsing error when the file reading reaches the
>  def detectip():
> sentence .
> You can execute the command line as long as it is a single line
> command
> 
> How to Get arround this ?

Since you're not showing precisely the code that's executing and
precisely the error you're getting, I can't even understand what you're
talking about.

-- 
   Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ 
\__/ Do not stand in a place of danger trusting in miracles.
    -- (an Arab proverb)
0
max78 (1220)
12/3/2003 11:34:20 AM
Jon Arter wrote:

> This results in a parsing error when the file reading reaches the
>  def detectip():
> sentence .
> You can execute the command line as long as it is a single line
> command
>
> How to Get arround this ?

if you're trying to write your own Python command-line interface,
use a real tool:

    http://www.python.org/doc/current/lib/module-code.html

if you're just trying to run code from a file, load the entire file
before you pass it to exec (or use execfile).

</F>




0
fredrik2101 (5275)
12/3/2003 11:47:49 AM
Jon Arter wrote:
> Hi
> Is is Possible to redirect the Python stdin in order to receive stdin
> commands from a file ?
> The time of redirection can  last until exiting the Python  and should
> be transparent for python it self.
> 
> with regards 
> Jon
> 
> 
> 
> 
If you're on *nix I know you can do this:

python myfile.py <somefile.txt

That would be transparent to your code.

Kris

0
12/3/2003 2:11:21 PM
Reply: