BlackAperture
BlackAperture

Reputation: 69

How can I run a os command without getting output to terminal?

When using subprocess or os libraries, executing the command returns the result in terminal. I want to be able to assign the output to a variable without getting any output returned to the terminal.

pid = subprocess.call(['pidof', process])

pid = os.system('pidof ' + process)

I only want to assign the variable pid, not return text to terminal. I was using the 'commands' library earlier, however it is not supported by python3.

Upvotes: 0

Views: 89

Answers (2)

Pratibha
Pratibha

Reputation: 1786

You can try check_output.

import subprocess
output = subprocess.check_output("COMMAND_TO_EXECUTE", shell=True)

Upvotes: 1

hunzter
hunzter

Reputation: 598

Have you tried redirect the command output to DEVNULL?

FNULL = open(os.devnull, 'w')
retcode = subprocess.call(['pidof', process], stdout=FNULL, stderr=subprocess.STDOUT)

Upvotes: 0

Related Questions