Much of the material in this document is taken from Appendix H.3 in the book A Primer on Scientific Programming with Python, 4th edition, by the same author, published by Springer, 2014.
The simplest way of running another program from Python is to
use os.system
:
cmd = 'python myprog.py 21 --mass 4' # command to be run
failure = os.system(cmd)
if failure:
print 'Execution of "%s" failed!\n' % cmd
sys.exit(1)
The recommended way to run operating system commands is to use
the subprocess
module. The above command is equivalent to
import subprocess
cmd = 'python myprog.py 21 --mass 4'
failure = subprocess.call(cmd, shell=True)
# or
failure = subprocess.call(
['python', 'myprog.py', '21', '--mass', '4'])
The output of an operating system command can be stored in a string object:
try:
output = subprocess.check_output(cmd, shell=True,
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError:
print 'Execution of "%s" failed!\n' % cmd
sys.exit(1)
# Process output
for line in output.splitlines():
...
The stderr
argument ensures that the output
string contains everything that the command cmd
wrote
to both standard output and standard error.
The constructions above are mainly used for running stand-alone programs.
Any file or folder listing or manipulation should be done by the
functionality in the os
and shutil
modules.
Given data/file1.dat
as a file path relative to the
home folder /users/me
($HOME/data/file1.dat
in Unix).
Python has tools for extracting the complete folder name
/users/me/data
, the basename file1.dat
, and the extension
.dat
:
>>> path = os.path.join(os.environ['HOME'], 'data', 'file1.dat')
>>> path
'/users/me/data/file1.dat'
>>> foldername, basename = os.path.split(path)
>>> foldername
'/users/me/data'
>>> basename
'file1.dat'
>>> stem, ext = os.path.splitext(basename)
>>> stem
'file1'
>>> ext
'.dat'
>>> outfile = stem + '.out'
>>> outfile
'file1.out'