Suppress Console Output in Python

Python interpreter uses the following three file objects on the sys library for standard input, output, and errors (exceptions):

  • sys.stdin is used for input from the input() function call,
  • sys.stdout – used for output of print() function and the prompt in the input(),
  • sys.stderr – Python interpreter’s own prompts (like help() function output), errors and exceptions go to this file object.

Both sys.stdout and sys.stderr print to the output console; the key difference is that the former prints the standard output, but the latter prints errors and exceptions.

This article discusses how to prevent Python from writing output into the console. We will do that in two ways: silence sys.stdout and sys.stderr or redirect the output into a file. Let’s work on examples.

Example 1: Suppress print() Function Output

The print() function contains file argument that determines the file object to send our output. If it is not given, it uses sys.stdout. The two print statements in the code example below are equivalent; they print the given string on the console.

We can send the output into a file with the following example. The print statement won’t write anything into the console.

If we don’t want to write the output to the console or a physical file, we can write the output to the null device (/dev/null in UNIX and nul in Windows). Anything written to the null device will be discarded and forgotten. In Python, the null device is available on os.devnull.

We can suppress the console output by writing the output to the null device, as shown in the code below.

The alternative to os.devnull is io.StringIO() – in-memory file-like object to hold the output without displaying it on the console.

Unlike os.devnull, which is not readable, io.StringIO() will hold our data on the memory buffer, and we can access it within the current interpreter session.

Example 2: Suppressing Console Output for a Large Block of Code (for Python 3.4+)

In Example 1, we saw how to suppress the console output within the print statement. The weakness of that example is that we need to redirect output for each print statement.

Let’s fix that. We want to suppress the console output from a block of code instead of doing it line by line. The contextlib package comes in handy here.

Remember: The following code example works in Python 3.4+. If you are running an older version, check Example 3.

Output (on the console):

35
But you can see this because it is outside redirecting context manager.

The output within the redirect_stdout context manager is suppressed in the example above. Alternatively, we can send the output to a file.

Example 3: Suppressing Console Output on Python 3.3 and Older

The contextlib.redirect_output used in Example 2 was introduced in Python 3.4. You can use this code if you are running Python 3.3 and older.

Output (on the console):

20
But you can see this on the console.
42
This will be printed on the console.

Example 4: Suppressing Console Output from subprocess

You will get some output on the console if you run terminal commands from within Python using a subprocess. This output may emanate from sys.stdout or sys.stderr. This means we need to redirect the output from both of those streams. Here is how to send the output to a file.

You can also suppress it by sending the output to subprocess.DEVNULL (it calls os.devnull under the hoods).

Conclusion

This article discussed how to suppress console output in Python by redirecting contents of the output dreams (sys.stdout and sys.stderr). In all four examples presented, the gist is to redirect the output to a file or os.devnull (a null device that automatically discards all the contents written to it).