之前一直在用执行Linux命令的函数,完成简单的封装,传入命令即可执行,可以返回是否执行成功。
def exe_cmd(cmd): p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: next_line = p.stdout.readline() return_line = next_line.decode("utf-8", "ignore") if return_line == '' and p.poll() != None: break print(return_line) stdout, stderr = p.communicate() if p.returncode != 0: return False return True