One - One Code All

Blog Content

python执行命令subprocess,Popen,PIPE

Python   2017-07-15 18:26:29

python调用shell 或cmd命令。

from subprocess import Popen, PIPE
def call_python_cmd(cmd):
    """调用执行python cmd"""
    print(cmd)
    try:
        p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
        p.wait()
        if p.returncode != 0:
            print(f"p_returncode:{p.returncode}")
            for line in p.stderr.readlines():
                print(line)
            return False
        for line in p.stdout.readlines():
            print(line)
    except Exception as e:
        print(e)
        return False
    return True



上一篇:pandas写入读取h5文件
下一篇:在Mac上删除自己安装的Python

The minute you think of giving up, think of the reason why you held on so long.