Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directing print output to a .txt file

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. 
like image 208
Clone Avatar asked Apr 12 '16 11:04

Clone


People also ask

How do you write output to a text file?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!

How do I save output as text in Python?

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 like in terminal?

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.

How to redirect output for all prints in Python?

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.

How do I specify the file to be printed in Python?

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.

How to specify the file to print in the call to print?

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.


2 Answers

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 a write(string) method; if it is not present or None, sys.stdout will be used.

And the documentation for open:

Open file and return a corresponding file object. If the file cannot be opened, an OSError 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) 
like image 99
Aaron Christiansen Avatar answered Sep 29 '22 16:09

Aaron Christiansen


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.") 
like image 29
Roman Bronshtein Avatar answered Sep 29 '22 16:09

Roman Bronshtein