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
  python interpreter won't import packages greenpy 1 387 Sep-11-2020, 07:47 PM
Last Post: buran
  How could i change the python interpreter path of os.system() 12019202386 2 519 Sep-02-2020, 06:58 AM
Last Post: DeaD_EyE
  Output CSV file with filepath and file contents glittergirl 1 457 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  Python Interpreter ankitdixit 3 806 Jul-10-2020, 04:15 PM
Last Post: Marbelous
  Question re: running code in Editor (i.e. Atom) vs Python interpreter rok 3 646 Jun-29-2020, 09:20 PM
Last Post: bowlofred
  csv file output rturus 7 996 Jan-30-2020, 02:09 PM
Last Post: buran
  File name as part of output file name Jeeping_Coder 1 591 Jan-10-2020, 03:43 PM
Last Post: Clunk_Head
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 626 Oct-21-2019, 01:01 PM
Last Post: enyrb
  python file output to log file Rsh 4 1,400 Aug-13-2019, 09:00 PM
Last Post: DeaD_EyE
  Output SQL to csv or xls file? JP_ROMANO 4 910 Aug-02-2019, 01:58 AM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

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