0

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.

BlackAperture
  • 69
  • 1
  • 8
  • Possible duplicate of [Python: How to get PID by process name?](http://stackoverflow.com/questions/26688936/python-how-to-get-pid-by-process-name) – Alex Fung Mar 02 '17 at 02:45

2 Answers2

1

You can try check_output.

import subprocess
output = subprocess.check_output("COMMAND_TO_EXECUTE", shell=True)
Pratibha
  • 1,730
  • 7
  • 27
  • 46
0

Have you tried redirect the command output to DEVNULL?

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