Python Forum
Python interpreter - output to file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python interpreter - output to file
#1
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.
Reply
#2
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/
Reply
#3
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?
Reply
#4
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")
Reply
#5
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
Reply
#6
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()
Reply
#7
Thank you bowlofred, this worked perfectly.
Reply
#8
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')
Reply
#9
OK cheers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Mac default python interpreter Viewpoint8455 2 874 Oct-13-2022, 06:25 AM
Last Post: perfringo
  Python Interpreter ankitdixit 4 2,620 Jun-10-2022, 12:37 PM
Last Post: katy1234
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 6,689 Mar-02-2022, 01:23 AM
Last Post: tomtom
  How to set Tab size to 4 in Python interpreter? zzzhhh 1 1,796 Jan-18-2022, 12:11 PM
Last Post: snippsat
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,473 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
Photo how I write the output into xml file in python? 3lnyn0 1 2,167 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  Showing and saving the output of a python file run through bash Rim 3 2,372 Oct-06-2021, 10:48 AM
Last Post: gerpark
  PyCharm One Interpreter for every file? muzikman 3 1,828 Sep-28-2021, 02:09 AM
Last Post: snippsat
  python interpreter won't import packages greenpy 1 1,915 Sep-11-2020, 07:47 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020