C:\...\PP4E\System\Media> start lp4e-preface-preview.html
C:\...\PP4E\System\Media> start ora-lp4e.jpg
C:\...\PP4E\System\Media> start sousa.au
Because the start command can run any file and command line, there is no reason it
cannot also be used to start an independently running Python program:
C:\...\PP4E\System\Processes> start child.py 1
This works because Python is registered to open names ending in .py when it is installed.
The script child.py is launched independently of the DOS console window even though
we didn’t provide the name or path of the Python interpreter program. Because
child.py simply prints a message and exits, though, the result isn’t exactly satisfying: a
new DOS window pops up to serve as the script’s standard output, and it immediately
goes away when the child exits. To do better, add an input call at the bottom of the
program file to wait for a key press before exiting:
C:\...\PP4E\System\Processes> type child-wait.py
import os, sys
print('Hello from child', os.getpid(), sys.argv[1])
input("Press <Enter>") # don't flash on Windows
C:\...\PP4E\System\Processes> start child-wait.py 2
Now the child’s DOS window pops up and stays up after the start command has
returned. Pressing the Enter key in the pop-up DOS window makes it go away.
Using start in Python scripts
Since we know that Python’s os.system and os.popen can be called by a script to run
any command line that can be typed at a DOS shell prompt, we can also start inde-
pendently running programs from a Python script by simply running a DOS start
command line. For instance:
C:\...\PP4E\System\Media> python
>>> import os
>>> cmd = 'start lp4e-preface-preview.html' # start IE browser
>>> os.system(cmd) # runs independent
0
The Python os.system calls here start whatever web page browser is registered on your
machine to open .html files (unless these programs are already running). The launched
programs run completely independent of the Python session—when running a DOS
start command, os.system does not wait for the spawned program to exit.
262 | Chapter 5: Parallel System Tools