Python uses the following two file objects on the sys library as output streams:
- sys.stdout – used for output of print() function and the prompt in input() function,
- sys.stderr – is an output stream for Python’s prompts (like the help() function), errors, and exceptions.
This article will focus on redirecting sys.stdout into a file instead of printing the output on the console using the following methods.
- Method 1: Redirecting print() function output by passing file argument,
- Method 2: Modifying the sys.stdout
- Method 3: Redirecting using contextlib.redirect_stdout() on Python 3.4 +
- Method 4: Using custom context manager in contextlib for Python 3.3 and older, and
- Method 5: Shell redirection.
Method 1: Redirecting output of print statement to a file
The print() function contains a file argument, which determines where to send output. If we want to send the output of a print statement to a file, we assign the argument a path to a writable object. For example,
1 2 3 4 5 6 |
with open("code1.log", "w") as f: # The output of the following two lines goes to the log file. print("This goes to the code1.log file", file=f) print("This line will also go the file", file=f) # This one is not redirected. It goes to stdout. print("This will be printed on the stdout") |
The first two print outputs are sent to the code1.log file, and the last one is not redirected – it’s sent to the stdout.
Method 2: Modifying sys.stdout
The sys.stdout, by default, sends output to the console. If we want to send the output to a file instead, we need to assign sys.stdout a writable file path, as shown below (please read the comments).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import sys # Anything output coming from the lines after the following line # is sent to the "redirect2.log" file sys.stdout = open("redirect2.log", "w") print("This is line 1\nThis is line 2") print("Colombia") # Close the sys.stdout as you would do a regular file. sys.stdout.close() # If we use print statement after the closing sys.stdout, we get an error # because the print statement won't know where to send the output. We need to # reset the sys.stdout preference. # The following line resets the sys.stdout modification to the default behaviour. # After this line, the output will be printed on the console. sys.stdout = sys.__stdout__ print("Line 1 1234") # to the console. |
Note: After modifying sys.stdout to write output to a file, you may need to reset its behavior by using the following line to go back to sending the output to the console (see the code above).
1 |
sys.stdout = sys.__stdout__ |
You can also use context manager to modify sys.stdout behavior.
1 2 3 4 5 6 7 8 9 10 |
<a id="post-2774-_7oi1jg56yv37"></a>import sys # Open redirect1.log on write mode as our stdout file object. with open("redirect1.log", "w") as sys.stdout: # print statements here will have there output sent to "redirect1.log" print("test line") print("test line 2") # Reset the sys.stdout preference. sys.stdout = sys.__stdout__ print("fdjfdskf") # printed on the console. |
Method 3: Redirecting using contextlib.redirect_stdout() on Python 3.4 +
The contextlib library contains a redirect_stdout() that comes in handy when redirecting Python output. The function was added in Python 3.4. Here is an example of how to use it to redirect output to a file.
1 2 3 4 5 6 7 8 9 10 11 12 |
<a id="post-2774-_wnw638ih2x9"></a>from contextlib import redirect_stdout # Sending the output to a file with open("code1.log", "w") as outfile: with redirect_stdout(outfile): # All contents returned by print statements in this block # will be written to "code1.log" file. print(help(abs)) m1 = 5 * 7 print(m1) print("You won't see this on the console because STDOUT is redirected.") print("This goes to the console because it is outside the redirect_stdout context.") |
Method 4: Using custom context manager in contextlib for Python 3.3 and older
The redirect_stdout() function discussed in Method 3 is not available in Python 3.3 and older. The alternative is to use the contextmanager decorator within the contextlib library, as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# For Python 3.3 and older from contextlib import contextmanager import sys # Using contextmanager decorator to enable us to call the redirect_output1() # function as a context manager using the "with" statement. @contextmanager def redirect_output1(file): # Open the file in write mode with open(file, "w") as f: # Change the sys.stdout to be the file passed sys.stdout = f try: yield f finally: # This part of the try-block will be executed # to set the sys.stdout to the original stdout file object. # Undoing the modification we have done to the stdout file object. sys.stdout = sys.__stdout__ # call redirect_output1 function as a context manager. with redirect_output1(file="code4.log"): # Send the output to a file. print(6 * 6) print("This string will be sent to a file instead of being sent to the console.")<a id="post-2774-_ynwygie6f0qy"></a> |
Method 5: Shell redirection
This method involves executing Python script within the command line and redirecting the output to a file using the “>” or “>>” operators.
The “>” overwrites a file, whereas “>>” appends the output to a file without overwriting the file (if it existed).
The advantage of this method is that unlike the other four methods discussed above, shell redirection does not require us to modify the code to achieve the purpose.
As an example, save the following code to the code23.py script
File: code23.py
1 2 3 4 5 |
from math import sqrt import random result = sqrt(76) - 5 / random.randint(0, 100) print(f"The result is {result}.") |
Then head to the terminal shell and execute the script above with the command:
1 |
python3 /path/to/code23.py > code3.log |
That will redirect the output of the script to the code3.log file. If code3.log exists at the time of execution, the file is overwritten.
Alternatively, you can use the “>>” operator to send the output to a file.
1 |
python3 /path/to/code23.py >> code4.log |
If code4.log already exists, the script’s output is appended to the end of the file without overriding the original content.
Conclusion
This article discussed five ways of redirecting the output of a Python script to a file. The first method covered how to redirect the output of a print statement to a file, and method 2 discussed how to modify sys.stdout to enable output redirection to a file.
Methods 3 and 4 explained using the contextlib library to redirect output for different versions of Python.
The last method explained how to redirect the output of a Python script during the execution from the shell.