Reputation: 69
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
Reputation: 1786
You can try check_output.
import subprocess
output = subprocess.check_output("COMMAND_TO_EXECUTE", shell=True)
Upvotes: 1
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