3

他のスクリプトからサブプロセスを使用して 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"])

スクリプト 2:

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.")
4

2 に答える 2

2

デフォルトで非ブロックのコードを使用しsubprocess.Popenているため、ユーザーからの入力を待つのではなく、カーソルの移動をプログラムします。しかし、必要なのは、プログラム カーソルをブロックしているコードです。subprocess.call正確にそれを行います。他のコマンドが完全に実行されるのを待っています。

subprocess.call問題を解決するために使用する必要があります。これでscript2ファイルを変更するだけです

import subprocess
p = subprocess.call(["python", "abca.py"])

subprocess.Popenとの違いについてはsubprocess.callこの回答で詳しく読むことができます。どのコマンドをいつ使用するか、およびそれらの違いについて説明します

于 2018-08-20T11:03:43.197 に答える