Python Forum
Python interpreter - output to file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python interpreter - output to file
#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


Messages In This Thread
RE: Python interpreter - output to file - by deanhystad - Apr-11-2020, 01:30 PM

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 2,091 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Mac default python interpreter Viewpoint8455 2 1,664 Oct-13-2022, 06:25 AM
Last Post: perfringo
  Python Interpreter ankitdixit 4 3,739 Jun-10-2022, 12:37 PM
Last Post: katy1234
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 11,966 Mar-02-2022, 01:23 AM
Last Post: tomtom
  How to set Tab size to 4 in Python interpreter? zzzhhh 1 2,589 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 2,168 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  Formatting to output file Mark17 2 1,972 Jan-13-2022, 09:06 PM
Last Post: BashBedlam
Photo how I write the output into xml file in python? 3lnyn0 1 3,965 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  Showing and saving the output of a python file run through bash Rim 3 5,486 Oct-06-2021, 10:48 AM
Last Post: gerpark
  PyCharm One Interpreter for every file? muzikman 3 2,590 Sep-28-2021, 02:09 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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