python 获取 shell 命令行执行结果

时间:2021-08-23 16:37:19   收藏:0   阅读:10
import subprocess as sp

# windows
windows = "gbk"

# linux
linux = "utf-8"

def exec(cmd, agent) -> None:
    p = sp.Popen(cmd, stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.STDOUT, shell=True)
    out, err = p.communicate()
    if p.returncode:
        print("code: {}, response:{} ".format(p.returncode, out.decode(agent)))

exec("ls -l", linux)

原文:https://www.cnblogs.com/test-works/p/15176030.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!