I have a Fortran Executable file named chuck
in the folder /home/debajyoti/chuckDir/
.
The program chuck
do is like, it takes an input file inputfile.txt
and an output file outputfile.txt
and calculates on the data from inputfile.txt
and writes the output in the file outputfile.txt
.
I to do all these calculations with the following steps in Ubuntu Terminal:
~$ cd chuckDir
~/chuckDir$ ./chuck <inputfile.txt> outputfile.txt
NOW I want to run chuck
and do all these within a Python Script
. The purpose of the Python3 Script
is, it takes the data from the outputfile.txt
to Plot. Now my question is how can I run chuck within the Python3 Script
itself?
This is usually where
subprocess
is a very useful bit of Pythonic magic.Assuming the input file is named
inputfile.txt
you can do this and throw stderr output on error, and stdout on successful run:However, you need to really make sure that you are using proper paths for your files and the
chuck
executable in this, and use full directory paths. This is the basic code you'll need to work with it and to executechuck
with proper paths. You should use fully qualified paths on disk where possible, by the way.Essentially the extra steps to check the returncode, stderr output, and stdout output from the program are debugging steps. If the program runs as intended, you will get an outputfile.txt where you specify it to go.