[Tutor] getting input for stdin

Alan Gauld alan.gauld at btinternet.com
Mon Dec 8 09:39:10 CET 2014


On 08/12/14 08:14, Alan Gauld wrote:

>> 1.What I want to do is to redirect the output of the program to a text
>> file instead of the standard Python output. That is why I opened the
>> text file. That part of the program works fine.

Thats OK, although its still usual to keep a reference to the real 
stdout in case you want to print to it.

>> 2. data is a py file named data.py which holds all the text
>> ...into a separate file which is loaded as a
>> module at the begining of the program. hence data.info

Your code doesn't show that?

>> <http://data.info> which is a list of strings which are printed to the
>> scree. Hence in my program I open a text file and send the print out
>> put to that which is then printed to the text file. This works too.

I don't understand how the <http://data.info> thing can work. Its not a 
string and not a valid variable name and a for loop won't work on a 
construct like that. OK, I just went back to your original message.
I see that the url is being generated by my mail reader from what is 
presumably an HTML post. You actually just had data.info in your code.
Apologies, but it shows the dangers of posting to a text based list 
using HTML... :-(

>> 3. When I ask for input in python the prompt which is usually in the
>> Python output console waits for the user input and returns that as a
>> string.
>> 4.Insted of 3 above I would like the user to be able to type into a
>> text file which may be opened by the program to collect input.

OK, Just to clarify. You expect the file to already exist before the 
program runs? How does Python learn the name of this file?
Normally you would pass it into the program as a parameter or use 
redirection.

In fact it sounds like redirection would solve most of your problems here.

You can write the program to output to normal stdout and read from 
normal stdin.

When you start the program you can redirect those at the command
line like:

$ python myscript >myoutfile.txt < myinfile.txt.

Would that do what you want?

>> instance i open a text file named give_me_your_input.txt and then the
>> user types his requirement in that text file which is taken as a
>> string by the program which in turn either returns the appropriate
>> answer.

This is the bit I don;t understand. It still sounds like you want the 
user to open the file and type in it while your python code is running?
Can you elaborate on how that works? How exactly would your user 
interact with the computer in this scenario?

>> I know how to do everything else above except how to get the input
>> from the text file in real time.

raw_input can read from a file that already exists. It just reads one 
line at a time. You just need to modify sys,stdin in the same way you 
modified sys.stdout. Is that all you want? Or do you want raw_input to 
read changes in a file as its being edited by a user? That's much
harder - maybe impossible.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list