Categories
Python Answers

How to hide output of subprocess with Python?

Spread the love

Sometimes, we want to hide output of subprocess with Python.

In this article, we’ll look at how to hide output of subprocess with Python.

How to hide output of subprocess with Python?

To hide output of subprocess with Python, we can set stdout to subprocess.DEVNULL`.

For instance, we write

import os
import subprocess

c = subprocess.call(['echo', 'foo'], 
    stdout=subprocess.DEVNULL,
    stderr=subprocess.STDOUT)

to output the echo command’s output to dev null by setting the stdout to subprocess.DEVNULL when we call subprocess.call.

Conclusion

To hide output of subprocess with Python, we can set stdout to subprocess.DEVNULL`.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *