[Tutor] Python Script Testing

Alex Kleider akleider at sonic.net
Thu Mar 15 13:50:03 EDT 2018


On 2018-03-15 09:56, Preeti Saxena wrote:
> Hi,
>    I am new to python. I am trying to test my program, like a coursera
> submission, which takes run time arguments using "raw_input()". Is 
> there a
> way I can write all the input data into a file and pass it from command
> line to the script?
> 1. I have to use "raw_input".
> 2. I do not wish to open a file inside the script as that will beat the
> purpose.

If you are using Linux or other UNIX like system (such as a Mac) the 
'HERE Document' feature [1] of the OS might prove useful:

# File: dummy.py
inputs = {}
inputs["first"] = raw_input("")
inputs["second"] = raw_input("")
for key in inputs.keys():
     print inputs[key]

Then from the command line:
alex at x301n4:~$ python dummy.py << HERE
first input
second input
HERE

On my system[2] the result is:
second input
first input

[1] http://tldp.org/LDP/abs/html/here-docs.html
[2] Ubuntu 16.4LTS


More information about the Tutor mailing list