我想从子进程使用Popen 执行命令:“python3”测试.py'
# The following is test.py code:
string = input('Enter Something')
if string == 'mypassword':
print('Success')
else:
print('Fail')
在我的程序中,我想执行'python3'测试.py'多次,每次提供输入,读取输出('成功'或'失败'),并将其存储在变量中。你知道吗
我的程序应该执行“python3”测试.py'如下所示:
from subprocess import Popen, PIPE
# Runs test.py
command = Popen(['python3', 'test.py'], stdin=PIPE)
# After this, it prompts me to type in the input,
# but I want to supply it from a variable
# I want to do something like
my_input = 'testpassword'
command.supplyInput(my_input)
result = command.getOutput()
# result will have the string value of 'Success' or 'Fail'
您可以将参数
stdout=PIPE
添加到Popen
,然后使用Popen.communicate
提供输入并读取输出。你知道吗有关更多详细信息,请阅读
Popen.communicate
的文档: https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate相关问题 更多 >
编程相关推荐