之前一直在用python调用shell命令,但是没有想过如何获取命令的返回值,现将方法记录如下。

#!/usr/bin/env python3
# encoding: utf-8
# coding style: pep8
# ====================================================
#   Copyright (C)2020 All rights reserved.
#
#   Author        : xxx
#   Email         : xxx@gmail.com
#   File Name     : get_cmd_result.py
#   Last Modified : 2020-04-01 11:57
#   Description   :
#
# ====================================================
import os

def lm_get_cmd_result(cmd):
    content = os.popen(cmd).read()
    print("cmd result:",content)