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?

3
  • 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. Commented Oct 11, 2016 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. Commented Oct 11, 2016 at 16:28
  • 1
    Got it... I shouldn't print the function. Just calling it is enough. Commented Oct 11, 2016 at 16:31

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.