Redirect Stdout to a File in Python

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,

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).

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).

You can also use context manager to modify sys.stdout behavior.

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.

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.

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

Then head to the terminal shell and execute the script above with the command:

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.

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.