473,410 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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


Jul 18 '05 #1
5 3021
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 && ma*@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
Jul 18 '05 #2
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 <ma*@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')


Jul 18 '05 #3
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 && ma*@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)
Jul 18 '05 #4
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>


Jul 18 '05 #5
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

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jan Knop | last post by:
Hello I am writing a Windows application where I need to redirect stdin, stdout and stderr from Python. to my application Is it a simple way of do it ? Has anyone done it using Winsock ?
1
by: aurora | last post by:
On Windows (XP) with win32 extension installed, a Python script can be launched from the command line directly since the .py extension is associated with python. However it fails if the stdin is...
0
by: Christophe HELFER | last post by:
hi, I have some problem with redirecting input and output from a process. I can only use VB language (sorry...) Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) ...
6
by: Christophe Helfer | last post by:
hi, I have some problem with redirecting input and output from a process. Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) command line utility as redirecting its...
3
by: Jan Danielsson | last post by:
Hello, I thought I'd write a program to collect information from pf (packet filter) and insert it into a postgresql database for review on a web page. First I checked if this has been done already,...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
1
by: pp4175 | last post by:
Hello Everyone, I am currently working on writing a GUI wrapper for a Menu driven DOS Program. What I was looking on doing is redirecting stdout/stdin to a stream and read/write similar to a...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
1
by: andy | last post by:
Can anyone tell me what I am doing wrong with the following code? When python 2.4 is embedded it crashes because of the assignment to stdin. import sys; class RedirectB: def readline(self):...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.