python - 从终端返回输出

标签 python python-3.x ubuntu operating-system

我想问一下是否有办法从终端向 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)

关于python - 从终端返回输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57238812/

相关文章:

python - 如何从 virtualenv 迁移到 buildout?

python - 在 SQLAlchemy 中选择 *?

python - 在同时安装了 2.x 和 3.x 的环境中安装 Python 包

Python - 音频文件未关闭 -> os.remove()

python - 合并具有相同日期的行并在 Pandas 中添加计数器列

ubuntu - Sed 和重命名文件

python - 从canny图像中查找矩形对象

Python列表转换成整数

Android在ubuntu上失败

c - 使用 gcc 编译/转换,matrix.h : No such file or directory