image

We always use print statements in Python to display the output in the console or command line terminal. However, sometimes we want to change this behavior to print to a text file instead of the console.

The print() method accepts the file parameter as one of the arguments, and using this, we can print the standard output to a file.

The default value of the file argument is sys.stdout, which prints the output on the screen.

Example 1: Print to the text file

The below program opens the existing text file in the write mode using the open() function (if file is not available it will create a new file) and writes all the text specified inside the print statement. Once the content is written to a file, we close the file using the close() method.

# Print to the text file
file = open('log.txt', 'w')
print('This is a sample print statement', file = file)
print('Hello World', file = file)

file.close()

Output

This is a sample print statement
Hello World

Example 2: Print to the text file using inline file argument

If you want to control only a few print statements to output to a text file, you could inline the file argument inside the print statement, as shown below.

# Appends the print statement into a log.txt

print("Welcome to ItsMyCode", file=open('log.txt','a'))

Output

Welcome to ItsMyCode

Redirecting the Standard Output Stream to a file

Specifying the file argument helps debug the smaller programs but is not desirable in some situations. 

In this case, we can redirect all the standard output stream to a file. Setting the standard output to a file ensures that the text specified inside the print() function will be written to a file instead of displaying in the console. 

The standard output can be set in Python by importing the sys module and setting the stdout to a file or file-like object.

import sys

# Prints in a console/terminal
print('Default mode: Prints on a console.')

# Store the reference of original standard output into variable
original_stdout = sys.stdout 

# Create or open an existing file in write mode
with open('log.txt', 'w') as file:
    # Set the stdout to file object
    sys.stdout = file
    print('File Mode: Print text to a file.')
    
    # Set the stdout back to the original or default mode
    sys.stdout = original_stdout

print('Default Mode: Prints again on a console.')

Output

Hello Welcome to Python Tutorials !!!
File Mode: Print text to a file.
Default mode: Prints on a console.
Default Mode: Prints again on a console.

Redirect the Python Script Output to File

The easiest and dirty way is to redirect the Python script’s output directly from the command line window while executing the script. 

For example, if you have a Python script file named main.py with the following code.

We can redirect the output of the Python script using the right angle bracket, as shown below.

python3 main.py > output.txt  
print("Hello World !!!")

If you open the output.txt file, you can see the below content written into the text file.

Hello World !!!

Total

0

Shares