python 获取 shell 命令行执行结果

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)

python 获取 shell 命令行执行结果

上一篇:SpringBean的作用域


下一篇:抄的..