os.system(cmd)

直接调用os.system(cmd)即可。在一个python文件里,同时写多个命令,然后按顺序执行。

案例1(运行一个python文件,从前到后按顺序调用程序执行):

以下例子就是,执行顺序为preprocess.py -> train.py -> test.py -> postprocess.py。

import os

cmd = 'python preprocess.py'
os.system(cmd)
cmd = 'python train.py'
os.system(cmd)
cmd = 'python test.py'
os.system(cmd)
cmd = 'python postprocess.py'
os.system(cmd)

案例2(连续测试模型):

import os

for epoch in range(args.start_epoch, args.end_epoch + 1):
	template = 'CUDA_VISIBLE_DEVICES={} python test.py --dataset {} --net {} --load_dir {} --model_config {} --checksession {} --checkepoch {} --checkpoint {} --cuda --pos_r {}'
	cmd = template.format(args.gpu_id, args.dataset, args.net, args.load_dir, args.model_config, args.checksession, epoch, args.checkpoint, args.pos_r)
    os.system(cmd)
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐