Running Fortran executable within Python script
<p>I am writing a Python script with the following objectives:</p>

<ol>
<li>Starting from current working directory, change directory to child directory 'A'</li>
<li>Make slight adjustments to a fort.4 file</li>
<li>Run a Fortran binary file (the syntax of which is <code>../../../../</code> continuing until I hit the folder containing the binary); return to 2. until my particular objective is complete, then</li>
<li>Back out of child directory to parent, then enter another child directory and return to 2. until I have iterated through all the folders in question.</li>
</ol>

<p>The code is coming along well. I am having to rely heavily upon Python's OS module for the directory work. However, I have never had any experience a) making minor adjustments of a file using python and b) running an executable. Could you guys give me some ideas on Python modules, direct me to a similar stack source etc, or perhaps give ways that this can be accomplished? I understand this is a vague question, so please ask if you do not understand what I am asking and I will elaborate. Also, the changes I have to make to this fort.4 file are repetitive in nature; they all happen at the same position in the file.</p>

<p>Cheers</p>

<p>EDIT::
entire fort.4 file:</p>

<pre><code>file_name           
movie1.dat       !name of a general file the binary reads
nbr_box          ! line3-8 is general info 
2
this_box
1
lrdf_bead
.true.
beadid1
C1               !this is the line I must change
beadid2
F4               !This is a second line I must change
lrdf_com
.false.
bin_width
0.04             
rcut
7                
</code></pre>

<p>So really, I need to change "C1" to "C2" for example. The changes are very insignificant to make, but I must emphasize the fact that the main fortran executable reads this fort.4, as well as this movie1.dat file that I have already created. Hope this helps</p>

<p>Ok so there is a few important things here, first we need to be able to manage our cwd, for that we will use the os module </p>

<pre><code>import os
</code></pre>

<p>whenever a method operates on a folder it is important to change directories into the folder and back to the parent folder. This can also be achieved with the os module.</p>

<pre><code>def operateOnFolder(folder):
     os.chdir(folder)
     ...
     os.chdir("..")
</code></pre>

<p>Now we need to do some method for each directory, that comes with this,</p>

<pre><code>for k in os.listdir(".") if os.path.isdir(k):
    operateOnFolder(k)
</code></pre>

<p>Finally in order to operate on some preexisting FORTRAN file we can use the builtin file operators.</p>

<pre><code>fileSource = open("someFile.f","r")
fileText = fileSource.read()
fileSource.close()
fileLines = fileText.split("\n")
# change a line in the file with -&gt; fileLines[42] = "the 42nd line"
fileText = "\n".join(fileLines)
fileOutput = open("someFile.f","w")
fileOutput.write(fileText)
</code></pre>

<p>You can create and run your executable <code>output.fx</code> from <code>source.f90</code>::</p>

<pre><code>subprocess.call(["gfortran","-o","output.fx","source.f90"])#create
subprocess.call(["output.fx"])                             #execute
</code></pre>

python fortran