The system function runs a new command under the current process,
and waits for it to finish.
Example: Using the os module to run an operating system command
# File: `os-example-8.py <os-example-8.py>`__
import os
if os.name == "nt":
command = "dir"
else:
command = "ls -l"
os.system(command)
-rwxrw-r-- 1 effbot effbot 76 Oct 9 14:17 README
-rwxrw-r-- 1 effbot effbot 1727 Oct 7 19:00 SimpleAsyncHTTP.py
-rwxrw-r-- 1 effbot effbot 314 Oct 7 20:29 aifc-example-1.py
-rwxrw-r-- 1 effbot effbot 259 Oct 7 20:38 anydbm-example-1.py
...
The command is run via the operating system’s standard shell, and
returns the shell’s exit status. Under Windows 95/98, the shell is
usually command.com whose exit status is always 0.
Warning: Since os.system passes the command on to the shell as
is, it can be dangerous to use if you don’t check the arguments
carefully (consider running os.system(“viewer %s” % file) with
the file variable set to “sample.jpg; rm -rf $HOME”). When
unsure, it’s usually better to use exec or spawn instead
(see below).
The exec function starts a new process, replacing the current one
( “go to process” , in other words). In the following example,
note that the “goodbye” message is never printed:
Example: Using the os module to start a new process
# File: `os-exec-example-1.py <os-exec-example-1.py>`__
import os
import sys
program = "python"
arguments = ["hello.py"]
print os.execvp(program, (program,) + tuple(arguments))
print "goodbye"
hello again, and welcome to the show
Python provides a whole bunch of exec functions, with slightly
varying behavior. The above example uses execvp, which searches
for the program along the standard path, passes the contents of the
second argument tuple as individual arguments to that program, and
runs it with the current set of environment variables. See the Python
Library Reference for more information on the other seven ways to call
this function.
Under Unix, you can call other programs from the current one by
combining exec with two other functions, fork and wait.
The former makes a copy of the current process, the latter waits for a
child process to finish.
Example: Using the os module to run another program (Unix)
# File: `os-exec-example-2.py <os-exec-example-2.py>`__
import os
import sys
def run(program, *args):
pid = os.fork()
if not pid:
os.execvp(program, (program,) + args)
return os.wait()[0]
run("python", "hello.py")
print "goodbye"
hello again, and welcome to the show
goodbye
The fork returns zero in the new process (the return from fork
is the first thing that happens in that process!), and a non-zero
process identifier in the original process. Or in other words, “
**not pid**” is true only if we’re in the new process.
System Message: WARNING/2 (/home/gerard/environments/sphinx-0.5-with-patch/thehazeltree/source/libref/os.rst, line 508); backlink
Inline strong start-string without end-string.
fork and wait are not available on Windows, but you can use
the spawn function instead. Unfortunately, there’s no standard
version of spawn that searches for an executable along the path,
so you have to do that yourself:
Example: Using the os module to run another program (Windows)
# File: `os-spawn-example-1.py <os-spawn-example-1.py>`__
import os
import string
def run(program, *args):
# find executable
for path in string.split(os.environ["PATH"], os.pathsep):
file = os.path.join(path, program) + ".exe"
try:
return os.spawnv(os.P_WAIT, file, (file,) + args)
except os.error:
pass
raise os.error, "cannot find executable"
run("python", "hello.py")
print "goodbye"
hello again, and welcome to the show
goodbye
You can also use spawn to run other programs in the background.
The following example adds an optional mode argument to the
run function; when set to os.P_NOWAIT, the script doesn’t
wait for the other program to finish.
The default flag value os.P_WAIT tells spawn to wait until the
new process is finished. Other flags include os.P_OVERLAY which
makes spawn behave like exec, and os.P_DETACH which runs
the new process in the background, detached from both console and
keyboard.
Example: Using the os module to run another program in the
background (Windows)
# File: `os-spawn-example-2.py <os-spawn-example-2.py>`__
import os
import string
def run(program, *args, **kw):
# find executable
mode = kw.get("mode", os.P_WAIT)
for path in string.split(os.environ["PATH"], os.pathsep):
file = os.path.join(path, program) + ".exe"
try:
return os.spawnv(mode, file, (file,) + args)
except os.error:
pass
raise os.error, "cannot find executable"
run("python", "hello.py", mode=os.P_NOWAIT)
print "goodbye"
goodbye
hello again, and welcome to the show
The following example provides a spawn method that works on either
platform:
Example: Using either spawn or fork/exec to run another program
# File: `os-spawn-example-3.py <os-spawn-example-3.py>`__
import os
import string
if os.name in ("nt", "dos"):
exefile = ".exe"
else:
exefile = ""
def spawn(program, *args):
try:
# check if the os module provides a shortcut
return os.spawnvp(program, (program,) + args)
except AttributeError:
pass
try:
spawnv = os.spawnv
except AttributeError:
# assume it's unix
pid = os.fork()
if not pid:
os.execvp(program, (program,) + args)
return os.wait()[0]
else:
# got spawnv but no spawnp: go look for an executable
for path in string.split(os.environ["PATH"], os.pathsep):
file = os.path.join(path, program) + exefile
try:
return spawnv(os.P_WAIT, file, (file,) + args)
except os.error:
pass
raise IOError, "cannot find executable"
#
# try it out!
spawn("python", "hello.py")
print "goodbye"
hello again, and welcome to the show
goodbye
The above example first attempts to call a function named spawnvp.
If that doesn’t exist (it doesn’t, in 2.0 and earlier), the
function looks for a function named spawnv and searches the path
all by itself. As a last resort, it falls back on exec and
fork.