Take the 2-minute tour ×

Is there a built-in method in Python to execute a system command without displaying the output? I only want to grab the return value.

It is important that it be cross-platform, so just redirecting the output to /dev/null won't work on Windows, and the other way around. I know I can just check os.platform and build the redirection myself, but I'm hoping for a built-in solution.

share|improve this question

2 Answers 2

up vote 12 down vote accepted
import os
import subprocess
subprocess.call(["ls", "-l"], stdout=open(os.devnull, "w"), stderr=subprocess.STDOUT)
share|improve this answer

You can redirect output into temp file and delete it afterward. But there's also a method called popen that redirects output directly to your program so it won't go on screen.

share|improve this answer

Your Answer


