for循环中的os.kill引发异常

时间:2018-09-23 21:38:35

标签: python windows operating-system

我有一个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循环中关闭子进程?

0 个答案:

没有答案