[Tutor] can i run the last saved input again

Alan Gauld alan.gauld at btinternet.com
Sat Jul 24 10:00:21 CEST 2010


"ANKUR AGGARWAL" <coolankur2006 at gmail.com> wrote

> hey this is a crazy question but i want to know it......
> suppose i have this code
>
> a=raw_input("enter the string :")
> print a
>
> now i want to ask is there's any way that python remembers the input 
> i gave
> it to last time and it just give me the output when i again run 
> python

No that is impossible, python is an interpreter and if it remembered
its input you would only ever be able to run one program. What you
want to do is get your program to remember its input, and that is
possible. It is very important you are clear in your mind what is 
python
and what is your code. You cannot change much in Python(*), you can
change anything in your code.

(*) Assuming we ignore hacking the Python source code of course!

All you need to do is save the input data to a file and when the 
program
starts, if the file exists read the input from that instead of stdin. 
You
may want to have a separate file for every user too...

You may want to add a flag to the program (-n say) that allows you to
specify that it should ignore the stored input should you wish to 
over-ride
that behaviour... You could also use a -f flag to force it to load a
particular input file...

Then the logic becomes:

if flag == 'f':
     datafilefile is command line arg
else datafile is userfile

if flag != 'n' and datafile exists:
    read input from datafile
else
   read input from user

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list