I have a for loop which opens a application with subprocess.Popen
and closes it with os.kill
.
Whole code:
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)
This throws for my this exception:
os.kill(programOpened.pid, signal.CTRL_C_EVENT)
SystemError: <built-in function kill> returned a result with an error set
Why is this exception thrown and how could i close a subprocess in a for loop?