I have a program that outputs many calculations and results to the console through the print statement. I want to write some code to export (or save) all the contents of the console to a simple text file.
I searched StackOverflow and other sites but I found some methods to redirect the print statement to print to a file directly, but I want the program to work normally, to display outputs to the console, then to save its contents AFTER all operations of the program done.
I am using PyCharm with Python2.7 if it matters
With all thanks and respect to all who contributed to this question. I have finally found a solution to this problem with minimal modifications to my original code. The solution is provided by the member @Status and here is its link .
Although I searched a lot before posting my question, but the answers of the respected members enlightened my mind to a precise search especially the contributions of @turkus, who performs an exceptional work, and @Glostas who opened my eyes to the "tee" which guided me to find the solution I posted (although it does not contain "tee").
The solution, as of the mentioned post with slight modifications:
1- Put the following Class in the program:
2- At the beginning of the program, before any print statements, put this line:
3- At the end of the program, after all of the print statements, put this line:
I tested this, and It works perfectly, and finally I have a clone of the console's output after the program ends.
With best regards to everybody, and Many thanks to all contributors.
You could override the
print
function which will still be accessible through thebuiltins
moduleEDIT: A similar solution for Python 2
This creates a
print
function that you use exactly as the function you import from__future__
.To close the file when everything is done you have to run:
I am unsure how you could receive the contents of a console for any editor however this can be achieved quite simply by replacing your
print()
statements with.write
There is a very obvious but not very elegant solution.
instead of:
you can make something like
finally just save sexport to a file
Maybe you should create a variable that will log the outputs and then put it into a file.
For ex:
Ok, so normally to get it done, you have to rewrite python
print
built-in function. But... There is ipython, which provides some hooks.First you need to have
ipython
installed:(I'm using sudo to simple locate then folder I need to reach, read further)
After ipython installation you'll have ipython extensions folder available, so get to it:
and create there let's say a file called
print_to_file.py
, here is its content:After saving a file just run:
Now get back to setting up our hook. We must open
ipython_config.py
created under path above and put there some magic (there is a lot of stuff there, so go to the end of file):After saving it, you can run
ipython
and write your code. Every your input will be written in a file under path you provided above, in my case it was:Notes
You can avoid loading your extension every time
ipython
fires up, by just delete'print_to_file'
fromc.InteractiveShellApp.extensions
list inipython_config.py
. But remember that you can load it anytime you need, just by typing inipython
console:Any change in
print_to_file.py
is being reflected in open ipython shell after using%reload_ext print_to_file
command, so you don't have to exit from and fire up it again.