python - 从终端返回输出
问题描述
我想问一下是否有办法从终端返回python终止值?例如,我正在使用os.system
将此行写入 ubuntu 终端
cat /sys/devices/virtual/thermal/thermal_zone*/temp
返回几个值,我希望能够在 python 代码中再次访问这些值,这可能吗?
解决方案
os.system
正在被弃用,或至少被替换为subprocess
您可以很容易地捕获输出subprocess.run
result = subprocess.run(['cat', '/sys/devices/virtual/thermal/thermal_zone*/temp'], capture_output=True)
print(result.stdout)