我有一个for循环,该循环用subprocess.Popen
打开一个应用程序,然后用os.kill
关闭它。
整个代码:
import subprocess
import time
import signal
import os
with open('programs.txt') as f:
programs = f.readlines()
programs = [x.strip() for x in programs]
for program in programs:
programOpened = subprocess.Popen([r"C:\\path\\to\\application\\" + program])
time.sleep(5)
os.kill(programOpened.pid, signal.CTRL_C_EVENT)
这引发了我这个异常:
os.kill(programOpened.pid, signal.CTRL_C_EVENT)
SystemError: <built-in function kill> returned a result with an error set
为什么会引发此异常?如何在for循环中关闭子进程?