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
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
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)