[Tutor] can i run the last saved input again

Lee Harr missive at hotmail.com
Sat Jul 24 17:34:20 CEST 2010

> 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
> then i type python prog.py
> output:
> enter the string:hello
> hello
> 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
> prog.py?

You've already received a number of different answers, so I will
tell you what I thought of when I read your (interesting, and not
really too crazy ;o) question:

Remember that input does not need to come from the keyboard.

I imagine that you are writing (and testing) a program, and you
are tired of always typing in the same input to test it.

One thing that you can do is put your input in to a separate file
and pipe that input in to your program to avoid all the typing...

My version of your program
$ cat echo.py 
a=raw_input("enter the string :")
print # Added a 2nd print, because ... well, you try it!
print a

Interactive ...
$ python echo.py 
enter the string :hello


Make some test input ...
$ cat> test_input_1
$ cat> test_input_2
some different way of saying hello

(or use your text editor to create the input files)

Now use your test input files ...
$ python echo.py < test_input_1
enter the string :
$ python echo.py < test_input_2
enter the string :
some different way of saying hello

If you are clever, there is probably a way to "tee" your
input as you type it so that it goes both to your program
and in to a separate file to save it for later.

Hotmail: Trusted email with powerful SPAM protection.

More information about the Tutor mailing list