[Tutor] Python Script Testing
Alan Gauld
alan.gauld at yahoo.co.uk
Thu Mar 15 13:41:54 EDT 2018
On 15/03/18 16: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?
In normal Python yes, you can simply pass the file name as an
argument to the program or you can use input redirection to
read the data from the file as if it were input by a user.
Unfortunately neither of these options is likely to work in
a web environment - one of the disadvantages of using such
a tool. The issue is not a Python one but one with your
tutorial. I suggest you contact the authors directly to
ask if they can suggest a solution (or incorporate
such a feature).
> 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.
In real-world python you could just use input redirection:
########## data.txt ##########
Alan
##############################
########## myscript.py #######
name = raw_input("What's your name? ")
print "Hello ", name
##############################
Executed with:
$ python myscript.py < data.txt
HTH
--
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