0

I have two python modules: buildContent.py which contains code that results in output i want. buildRun.py which i run in order to redirect the output to a file.

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

import buildContent
import sys

with open('out.xhtml', 'w') as f:
    sys.stdout = f
print buildContent

I can see my output in the console but the file result is:

<module 'buildContent' from 'here's my path to the file'>

what to do?

Arkady
  • 14,305
  • 8
  • 42
  • 46
dyb
  • 59
  • 8
  • 1
    You're trying to print the actual module. Did you want to call a function in `buildContent`? What you're trying to do at the moment is like doing `import sys; print sys`. – Farhan.K Oct 11 '16 at 16:13
  • That kinda opened my eyes... So i wrapped everything in a function then call it in `buildRun` and it works! Now the only problem is that i have `None` printed in the last line of the output file. – dyb Oct 11 '16 at 16:28
  • 1
    Got it... I shouldn't `print` the function. Just calling it is enough. – dyb Oct 11 '16 at 16:31

2 Answers2

1

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

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

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

see this question for an example

xgadam
  • 180
  • 1
  • 11
0

Instead of printing buildContent, just execute that module with the required parameters. Not sure of the content of buildContent but something like this should work:

buildContent(data)

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

var = buildContent(data)
print var

If you do not need any data atall to run buildContent, just run:

buildContent()
kskp
  • 692
  • 5
  • 11
  • 23