如何在python的子进程中执行一组语句?

2024-06-01 02:37:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个子进程中执行一组语句,这些语句是python脚本中shell和python代码的组合。我正在使用subprocess.call()方法,但它只接受一个shell命令作为输入。我想在子进程中的shell命令之后执行一些python代码,并在shell+python代码完成执行后退出子进程

command = "./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights {0}".format(latest_subdir)
proc = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
with open(result_file, 'w') as fout:
    fout.write(out)
os.system('mv {0} {1}'.format(latest_subdir,processed_dir))
s3_util.upload_results([result_file])

Tags: 代码命令format进程语句procshellout