当我使用其他脚本中的子进程启动python3脚本时,出现以下错误:
Select the keyword preset you want to use:Traceback (most recent call last):
File "test2.py", line 9, in <module>
keywordselect=input("Select the keyword preset you want to use:")
EOFError
但是当我使用python3 generate.py正常启动脚本时,它可以正常工作而没有错误。
脚本1:
import subprocess
p = subprocess.Popen(["python3", "test2.py"])
script2:
print("Keyword")
print("1. Preset1")
print("2. Preset2")
print("3. Preset3")
print("4. Preset4")
print("You can edit the presets in /presets/keywords/.")
selecting = 1
while selecting == 1:
keywordselect=input("Select the keyword preset you want to use:")
if keywordselect == "1":
print("You selected keyword preset 1.")
selectedkeywordlist = "presets/keywords/preset1.txt"
elif keywordselect == "2":
print("You selected keyword preset 2.")
selectedkeywordlist = "presets/keywords/preset2.txt"
elif keywordselect == "3":
print("You selected keyword preset 3.")
selectedkeywordlist = "presets/keywords/preset3.txt"
elif keywordselect == "4":
print("You selected keyword preset 4.")
selectedkeywordlist = "presets/keywords/preset4.txt"
else:
print("You didn't select a valid option, please try again.")
答案 0 :(得分:1)
您正在使用subprocess.Popen
,这是默认情况下的非阻塞代码段,因此您对光标进行编程而不是等待用户的输入。但是,您需要的是阻止程序光标的代码。 subprocess.call
确实做到了。它将等待另一个命令完全执行。
您需要使用subprocess.call
来解决问题。只需使用此更改您的script2文件
import subprocess
p = subprocess.call(["python", "abca.py"])
您可以在this answer中详细了解subprocess.Popen
和subprocess.call
之间的区别。哪个描述了何时使用哪个命令以及它们之间的区别
答案 1 :(得分:0)
您必须通过stdin
的调用来重定向stdout
和script2.py
,可以为此使用subporcess.PIPE:
p = subprocess.Popen(["python3", "test2.py"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)