如何在python脚本中向python脚本提供用户输入?

时间:2019-05-02 21:16:03

标签: python input subprocess pipe

我有一个具有多个用户输入的脚本(使用python输入功能),我想编写另一个脚本来执行前一个脚本并为其提供输入。

我尝试将子进程模块与Popen一起使用,但出现了完全不同的错误,即TypeError和EOF错误。

~$ python3 importPasswords.py 

Traceback (most recent call last):

File "importPasswords.py", line 68, in <module> child.communicate(input="3")
File "/usr/lib/python3.6/subprocess.py", line 843, in communicate stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.6/subprocess.py", line 1499, in _communicate input_view = memoryview(self._input)
TypeError: memoryview: a bytes-like object is required, not 'str'

~$ Traceback (most recent call last):

File "PasswordGeneratorConsole.py", line 188, in <module> pgc = PasswordGeneratorConsole()
File "PasswordGeneratorConsole.py", line 20, in __init__ self.start()
File "PasswordGeneratorConsole.py", line 24, in start choice = input("What do you want to do: [1] save a password, [2] generate a new password or [3] search a password: ")
EOFError: EOF when reading a line

第一个文件的输入行:

choice = input("What do you want to do: [1] save a password, [2] generate a new password or [3] search a password: ")

另一个文件:

from subprocess import Popen, PIPE

p = Popen(['python3', 'PasswordGeneratorConsole.py'], stdin=PIPE, stdout=PIPE)
p.communicate("3")

我希望第二个程序为第一个程序提供3作为输入。

0 个答案:

没有答案