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 @ 2023-07-01 09:05  CJTARRR  阅读(235)  评论(0)    收藏  举报