Issue
I am using Spyder for Pyhton and for some problems the output in the IPython console is quite large (more than 10000 lines) and it is quite inefficient to analyse the output directly in the IPython console. This is why I would like to know if there is a way how I can just print the output to a .txt file? I tried what was suggested here How to write console output on text file. So I use at the beginning of my code:
import sys
sys.stdout = open('C:/Users/Python_Code/log.txt', 'w')
and at the end sys.stdout.close()
. However, I get an error message stating "Value Error: I/O operation on closed file" and the log.txt file is not created. If I create the log.txt file manually in the corresponding folder, the file remains empty. Not only remains the file empty but also Spyder does not react at all after executing this code so I have to shut it down and restart.
Do you have any idea, how I can redirect the console output to a txt-file? I'd appreciate every comment and would be thankful for your help.
Solution
Put sys.stdout = sys.__stdout__
after you close the file.
Something is still trying to write to console, so you have to change it to the system stdout to prevent errors after closing. If you absolutely must capture these logs, you must use the python file.py > output.txt
on the command line.
Answered By - thshea
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.