You can use subprocess.check_output which will redirect stdout to the function call’s return value

Something like:

import subprocess

output = subprocess.check_output(['which', 'python'])
print(output)