Gilad Naor
Gilad Naor

Reputation: 21566

In Python - how to execute system command with no output

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.

Upvotes: 13

Views: 9248

Answers (2)

oefe
oefe

Reputation: 19926

import os
import subprocess
subprocess.call(["ls", "-l"], stdout=open(os.devnull, "w"), stderr=subprocess.STDOUT)

Upvotes: 26

vava
vava

Reputation: 25401

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.

Upvotes: 2

Related Questions