In [5]:
%%bash
# This is ipython cell magic that allows you to use bash commands throughout
# the rest of this cell within ipython.

date
pwd
ls
grep bash Using_bash_in_python_scripts.ipynb | grep individual

# Note 1: %%bash has to be the first entry in your cell
# .
# Note 2: if you want to run scripts that use ipython magic you can save them
# as ipython scripts and run them from terminal using ipython (instead of
# python). People without ipython will not be able to use your script though.
# .
# Note 3: this works for ANY OTHER INTERPRETER on your system (bash, ruby,
# perl, zsh, R, etc.) just use %%script perl, or %%script python3, etc


Wed Jan 13 14:05:11 PST 2016
/Users/houghb/Dropbox/Projects/PRG_tutorials/Python
PEP8_compliance_tips.ipynb
Using_bash_in_python_scripts.ipynb
      "      \"    \\\"### It is also possible to prepend individual bash commands with ! (instead of setting the entire cell to use bash)\\\\n\\\",\\n\",\n",
      "    \"### It is also possible to prepend individual bash commands with ! (instead of setting the entire cell to use bash)\\n\",\n"
    "grep bash Using_bash_in_python_scripts.ipynb | grep individual\n",
    "# It is also possible to prepend individual bash commands with ! (instead of \n",

In [23]:
# It is also possible to prepend individual bash commands with ! (instead of
# setting the entire cell to use bash). This is helpful if you want to store
# the output of something as a python object.  Again, this is ipython magic so
# won't work in .py scripts.

header = !head rateconstantlist.dat
print header


['1.00E+13 0 163254', '1.00E+13 0 163254', '1.00E+13 0 163254', '1.00E+13 0 163254', '1.00E+13 0 184184', '1.00E+13 0 188370', '1.00E+13 0 171626', '1.00E+13 0 179998', '1.00E+13 0 167440', '1.00E+13 0 121394']

In [ ]:
# If you need your bash commands to be able to run from a .py script (i.e. if
# you're not using ipython notebook) then use call:

# This executes a series of bash commands
from subprocess import call
call('make -f /scratch/jpfaendt/software/parest/ddasac/software/Makefile;'
     'cp -f %s ddat.in;'
     './parest -d ddat.in;'
     'mv graph.out %s/ddasac_results_1.out;'
     'mv net_rates.out %s/ddasac_net_rates_1.out;'
     'mv rates.out %s/ddasac_rates_1.out'
     % (ddat_outputfile, working_directory, working_directory,
        working_directory), shell=True)