I am trying to read input from the user and store it in a variable by using subprocess.check_output
in python2.7. But it shows up the error OSError: [Errno 2] No such file or directory
when I try to run it. Also it is to be noted that I strictly want to use shell=False
because of the security concerns.
I have tried subprocess.Popen
and it doesnt work that way too.
I have tried to use sys.stdin = open('/dev/tty', 'r')
and stdin=subprocess.PIPE
but give the same error as above.
>>> import sys
>>> import subprocess
>>> sys.stdin = open('/dev/tty', 'r')
>>> cmd = ('read userinput && echo "$userinput"')
>>> confirmation = subprocess.check_output(cmd.split(), stdin=sys.stdin).rstrip()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 567, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
The expected result is that it should ask for user input and store it to the confirmation
variable