Python Forum

Full Version: Python interpreter - output to file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
With the Python interpreter, analogous to the Command Prompt in windows, is there a way to export or write output to a file.

In looking at the code from the following link (about halfway down the page).
https://robbievanleeuwen.github.io/finit...-analysis/

There is this line of code which I want to write to file:

section.display_results()  # print the results to the terminal
Is there a way to do this?
The output consists of about 50-100 lines of data.
You can redirect standard output to a file, execute the command, than restore. You will find lots of tutorials and blog posts about how to do this, but I like this one because it provides examples of different ways it can be accomplished.

https://www.blog.pythonlibrary.org/2016/...ng-stdout/
Thanks for responding.

... Please ignore previous comments on errors if you read the post recently, as I have reinstalled Python, not using any spaces, and using this type of slash in file paths "/" ... though [writing after notes below] I'm now having issues reproducing the same output with the same code. Very frustrating.

The code below works, but I still don't know how to add the following line from the author's code, which consists of 50-100 lines of output.

section.display_results() # print the results to the terminal

import sys

def redirect_to_file(text):
	original = sys.stdout
	sys.stdout = open('C:/etc/sectionproperties/redirect.txt', 'w')
	print('This is your redirected text:')
	print(text)
	sys.stdout = original

print('This string goes to stdout, NOT the file!')
Otherwise, what should I search for in Google?
What exactly do you want to output?

In the code you linked to, it's importing a package that does multiple calculations and then when called, can print out the results. So you can't just take the "print everything out" line and put it in your own code.

If you're writing your own code, you'd normally either just print() it to the terminal, or open a file and write to the file. Can you give some more information on what you want to print out? Are you looking for something more involved than just

print("This data goes to the terminal")
Below is a sample of what will be printed out, showing the first 10 lines or so, the remainder is similar.
The values are calculated by the solver from the author.

Output:
A = 2.919699e+03 Qx = 2.919699e+05 Qy = 7.122414e+04 cx = 2.439434e+01 cy = 1.000000e+02 Ixx_g = 4.831277e+07 Iyy_g = 3.392871e+06 Ixy_g = 7.122414e+06 Ixx_c = 1.911578e+07 Iyy_c = 1.655405e+06 Ixy_c = -6.519258e-09
I see. You have the existing module and you just want to send it to a file rather than the screen. I didn't understand that part.

My first thought would be, can't you do this with your command interpreter or shell? In windows or linux, you should be able to do something similar to:

$ pythonprogram > outputfile
And your output is in a file without touching the code. That's the common way to do this.

If you really want to do it in the code (and not modify the module), you could hope it's just printing to STDOUT, and redirect it inside.

import sys

# redirect stdout to a file
sys.stdout = open("/my/output/file", "w")

# everything after this should go to the file.
print("Hello, world")
section.display_results()
Thank you bowlofred, this worked perfectly.
I used the contextlib library for my embedded console class but it works with anything that has a write method/function. It works really well with a context manager
from contextlib import redirect_stdout

print('This goes to console')
with redirect_stdout(open('junk.txt', 'w+')):
    print('This goes to a file')
    print('This is the last line to the file')
print('This is back in the console')
OK cheers.