NOTE: Many of the same questions have been asked about python raw_input() in sublime text. This question is NOT about sublime. The python code is called in Windows command prompt which unlike the sublime terminal does support interactive inputs.
I have a python program that takes user input with the built-in function raw_input(). See below.
def password_score():
pwd = raw_input('Enter a password: ')
gname = raw_input('Enter your first name: ')
...
I call the program in cmd by
echo password_score()|python -i a06q1.py
where a06q1.py is the file name. The path of the python directory has been added to system variable %PATH%
temporarily. I am in the directory of the file. My operating system is Windows 7. I am using python 2.6. The same command has worked until now.
Then cmd returns
File "<stdin>", line 1, in <module>
File "a06q1.py", line 27, in password_score
pwd = raw_input(p_prompt)
EOFError: EOF when reading a line
Is there a way to get around it within cmd?
EDIT: I just tried in on an iOS terminal too. With the same command as in cmd (with quotes), it returns the same error. Is there anything wrong about the command line I used? Thank you!
EDIT: Sebastian's answer solves the problem. The command should adapt to windows as follows.
printf "a06q1.password_score()\n'arg1\n'arg2"|python -i -c "import a06q1"
The single quotes succeeding \n
can be replaced by spaces. They separate multiple inputs.
EOF means that there is no more input. And it is true, the only line is consumed by -i
option:
$ echo "f()" | python -i -c "def f(): print('x'); input('y\n')"
>>> x
y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in f
EOFError: EOF when reading a line
>>>
Provide more input:
$ printf "f()\n'z'" | python -i -c "def f(): print('x'); print(input('y\n')*3)"
>>> x
y
zzz
>>>
As you said: it is "canonically bad" to specify a function to run in such manner. If you don't know in advance, what function you want to run then as an alternative, you could run it as:
$ python -c "from a06q1 import password_score as f; f()" < input_file.txt