Sunday, September 13, 2009

Using Python to start a process: Notepad as an example

This examples shows how to start a common process in windows while allowing the Python program to continue and, if needed, terminate the process.
import subprocess

notepadProcess = subprocess.Popen("Notepad")
while notepadProcess.poll() is None:
    print 'still running'
    reply = raw_input("Kill process ? (type yes) ")
    print '>>|'+reply.strip()+'|<<'
    if reply.strip()=='yes':
        print "closing process"
        notepadProcess.terminate()

No comments:

Post a Comment