Print output to a file using another Python module
<p>I have two python modules: <code>buildContent.py</code> which contains code that results in output i want. <code>buildRun.py</code> which i run in order to redirect the output to a file.</p>

<p>I'm trying to save the output from <code>buildContent.py</code> to a file and I did something like this in the <code>buildRun.py</code>:</p>

<pre><code>import buildContent
import sys

with open('out.xhtml', 'w') as f:
    sys.stdout = f
print buildContent
</code></pre>

<p>I can see my output in the console but the file result is:</p>

<pre><code>&lt;module 'buildContent' from 'here's my path to the file'&gt;
</code></pre>

<p>what to do?</p>

<p>the redirection is working properly. 
if you replace your print statement with a string you will see that it has 
worked.</p>

<p>The reason for that output is that you are not calling any functions within buildcontent, merely importing it. </p>

<p>The solution is to run the buildContent file from within the above where your print statement should be.</p>

<p>see <a href="https://stackoverflow.com/questions/1027714/how-to-execute-a-file-within-the-python-interpreter">this question</a> for an example</p>

python