I have looked at python's subprocess and os modules but have not been able to find a concrete example. Here is an example of what I want to do:

os.system(r"someprocess.exe")
time.sleep(10)
os.system(r"TASKKILL /F /IM someprocess.exe")

Currently after running the first line, the shell runs the process within it, and does not move forward. On the other hand, I want it to spawn a new process independent of this python shell, go to sleep, and then kill it after sometime. I am on a windows machine.

有帮助吗?

解决方案

Use subprocess.

import subprocess
import time

p = subprocess.Popen(['notepad.exe'])
time.sleep(10)
p.kill()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top