Is there a way to save all of the print output to a txt file in python? Lets say I have the these two lines in my code and I want to save the print output to a file named output.txt
.
print ("Hello stackoverflow!") print ("I have a question.")
I want the output.txt
file to to contain
Hello stackoverflow! I have a question.
the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!
To write to a file from within your script, user file I/O tools that are provided by Python (this is the f=open('file. txt', 'w') stuff. If don't want to modify your program, you can use stream redirection (both on windows and on Unix-like systems). This is the python myscript > output.
How to print outputs to text file same like I print outputs in terminal? You are reopening the file for each iteration of the loop so when you write to it you overwrite what is already there. You need to open the file outside of all the loops and open it in append mode, denoted by a.
To redirect output for all prints, you can do this: Show activity on this post. A slightly hackier way (that is different than the answers above, which are all valid) would be to just direct the output into a file via console. So imagine you had main.py and then text.log will get all of the output.
We can directly specify the file to be printed in the call to print (), by mentioning the file keyword argument. For example, the below snippet prints to the file output.txt.
Output (Assume that output.txt is a newly created file) We can directly specify the file to be printed in the call to print (), by mentioning the file keyword argument.
Give print
a file
keyword argument, where the value of the argument is a file stream. We can create a file stream using the open
function:
print("Hello stackoverflow!", file=open("output.txt", "a")) print("I have a question.", file=open("output.txt", "a"))
From the Python documentation about print
:
The
file
argument must be an object with awrite(string)
method; if it is not present orNone
,sys.stdout
will be used.
And the documentation for open
:
Open
file
and return a corresponding file object. If the file cannot be opened, anOSError
is raised.
The "a"
as the second argument of open
means "append" - in other words, the existing contents of the file won't be overwritten. If you want the file to be overwritten instead, use "w"
.
Opening a file with open
many times isn't ideal for performance, however. You should ideally open it once and name it, then pass that variable to print
's file
option. You must remember to close the file afterwards!
f = open("output.txt", "a") print("Hello stackoverflow!", file=f) print("I have a question.", file=f) f.close()
There's also a syntactic shortcut for this, which is the with
block. This will close your file at the end of the block for you:
with open("output.txt", "a") as f: print("Hello stackoverflow!", file=f) print("I have a question.", file=f)
You can redirect stdout into a file "output.txt":
import sys sys.stdout = open('output.txt','wt') print ("Hello stackoverflow!") print ("I have a question.")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With