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.

Run any operating system command

The simplest way of running another program from Python is to use os.system:

cmd = 'python 21 --mass 4'   # command to be run
failure = os.system(cmd)
if failure:
    print 'Execution of "%s" failed!\n' % cmd

The recommended way to run operating system commands is to use the subprocess module. The above command is equivalent to

import subprocess
cmd = 'python 21 --mass 4'
failure =, shell=True)

# or
failure =
            ['python', '', '21', '--mass', '4'])

The output of an operating system command can be stored in a string object:

    output = subprocess.check_output(cmd, shell=True,
except subprocess.CalledProcessError:
    print 'Execution of "%s" failed!\n' % cmd

# 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.

Split file or folder name

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
>>> foldername, basename = os.path.split(path)
>>> foldername
>>> basename
>>> stem, ext = os.path.splitext(basename)
>>> stem
>>> ext
>>> outfile = stem + '.out'
>>> outfile