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>Instead of printing <code>buildContent</code>, just execute that module with the required parameters. Not sure of the content of <code>buildContent</code> but something like this should work:</p>

<pre><code>buildContent(data)
</code></pre>

<p>This way the code inside <code>buildContent</code> will run on the <code>"data"</code> and print the results (if the print statements are given in the module). If you did not include print statements in <code>buildContent</code>, collect the output into a variable and print that variable. Something like this:</p>

<pre><code>var = buildContent(data)
print var
</code></pre>

<p>If you do not need any data atall to run <code>buildContent</code>, just run:</p>

<pre><code>buildContent()
</code></pre>

python