python执行终端命令并获得输出结果

  • 兼容windows和linux的终端执行函数

    def shell_exec(cmd: str) -> str:
       """
      执行终端命令,输出终端打印结果
      :param cmd:
      :return:
      """
       with os.popen(cmd) as fp:
           bf = fp._stream.buffer.read()
       out = bf.decode().strip()
       return out
posted on 2023-07-01 09:04  CJTARRR  阅读(193)  评论(0编辑  收藏  举报