redirecting Stdin

J

Jon Arter

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
 
E

Erik Max Francis

Jon said:
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 && (e-mail address removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ The map is not the territory.
-- Alfred Korzybski
 
J

Jon Arter

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
 
E

Erik Max Francis

Jon said:
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 && (e-mail address removed) && 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)
 
F

Fredrik Lundh

Jon said:
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>
 
K

Kristofer Wouk

Jon said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top