【发布时间】:2020-03-07 15:48:43
【问题描述】:
我正在尝试了解如何使用 python 子进程模块执行命令/程序并通过提供输入来响应提示。
可以接受多个输入的示例程序 Program1.py:
arr1=[]
username = input("Enter your username1 : ")
password = input("Enter your password1 : ")
arr1.append((username,password))
username = input("Enter your username2 : ")
password = input("Enter your password2 : ")
arr1.append((username,password))
username = input("Enter your username3 : ")
password = input("Enter your password3 : ")
arr1.append((username,password))
username = input("Enter your username4 : ")
password = input("Enter your password4 : ")
arr1.append((username,password))
for item in arr1:
print("username:",item[0],"Password:",item[1])
我编写了另一个名为 Execute_Program1.py 的程序
import subprocess
proc = subprocess.Popen('python Program1.py',stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE,shell=True,universal_newlines=True)
print(proc.poll())
while proc.poll() is None:
print(proc.stdout)
proc.stdin.write('inputdata\n')
但是这个程序无法执行 Program1.py。我已经阅读了许多与此相关的帖子。根据我得到的信息 proc.poll() return's None 那么 Popen 执行的命令仍然有效。 但是我的程序给出了以下输出:
None
<_io.TextIOWrapper name=4 encoding='cp1252'>
我使用的是 Python 版本 3.7.4。任何人都可以帮我输入一些我做错的地方吗? 提前致谢。
【问题讨论】:
-
请问您为什么要使用第二个 Python 脚本来执行第一个脚本,而这一切都可以在一个脚本中完成?
-
实际上第一个脚本需要由 Robot 框架执行,作为自动化的一部分。应自动提供第一个程序的输入。由于机器人框架没有可以执行和提供输入的库,我正在编写第二个程序作为机器人框架的库来执行此操作。
标签: python python-3.x