Python - How To Redirect Print Output to a File in Python

ID : 140

viewed : 71

Tags : PythonPython Print

vote vote

99

There is one more kind of task in file handling that can be done using python i.e redirecting output to an external file. Basically, a standard output can be printed to a file that is chosen by the user itself. There are many ways to carry this out.

In this tutorial, we will see some methods to redirect output to a file in Python.

Use the write() Function to Print Output to a File in Python

This is a built-in Python function that helps in writing or adding a specified text into a file. w and a are the 2 operations in this function that will write or add any text in a file. w is used when the user wants to empty the file before writing anything in it. Whereas a is used when the user just wants to add some text to the existing text in the file.

Example:

with open("randomfile.txt", "a") as o:     o.write('Hello')     o.write('This text will be added to the file') 

Note that open() function is used here to open the file. a in the code denotes that text is been added to the file.

Use the print() Function to Print Output to a File in Python

In this method, first, we call the open() function to open the desired file. After that print() function is used to print the text in the file. It is always the user’s choice to either use the w operator or the a operator.

Example:

with open("randomfile.txt", "w") as external_file:     add_text = "This text will be added to the file"     print(add_text, file=external_file)     external_file.close() 

Note that the close() function is also used to close the file in the above code after opening it with the open(). After calling the close() function, the file cannot be read and nothing else can be written. If the user tries to make any change in the file after calling the close() function, an error will be raised.

Use sys.stdout to Print Output to a File in Python

The sys module is a built-in Python module that is used by the user to deal with various parts of the runtime environment in Python. To use the sys.stdout, the sys module needs to be imported first.

sys.stdout is used when the user wants to display the output directly to the main console of the screen. The form of the output can be varied, for example, it can be a prompt for input, a print statement, or just an expression. In this method, we will print a statement in a text file.

Example:

import sys   file_path = 'randomfile.txt' sys.stdout = open(file_path, "w") print("This text will be added to the file") 

Note that before using the sys.stdout as an object to open and print the statement in the text file, a definite path of the file must be defined by the user otherwise, none of the operations can be performed on the file.

Use the contextlib.redirect_stdout() Function to Print Output to a File in Python

contextlib module is generally used with the with statement.

contextlib.redirect_stdout() function helps to redirect the sys.stdout to some file on a temporary basis by setting up a context manager.

Example:

import contextlib   file_path = 'randomfile.txt' with open(file_path, "w") as o:     with contextlib.redirect_stdout(o):         print("This text will be added to the file") 

As you can see, the with statement is used with the operations of contextlib module.

  • Related HOW TO?