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