python - Read inputs from stdin/file and raise errors for less inputs -
lets have simple python code this:
num1 = input() num2 = input() res = sum(num1,num2)
as per requirements, should able convert above code(that got input user) in such way, should able supply inputs predefined file. how ? , once add code, above works. if add input below "num3=input()", should raise error eoferror: eof when reading line. how raise error ?
num1 = input() num2 = input() num3 = input() res = sum(num1,num2)
please let me know questions.
well far know there's no way tell pythons input function read stdin. without rewriting code use file objects, redirect file stdin. send eof @ end of file , raise error you're expecting.
for example run:
python script.py < file.in
or if have shebang on first line of file , you've made executable:
./script.py < file.in
this assuming you're running linux based or posix shell.
Comments
Post a Comment