os.system(cmd)
直接调用os.system(cmd)即可。在一个python文件里,同时写多个命令,然后按顺序执行。
案例1(运行一个python文件,从前到后按顺序调用程序执行):
以下例子就是,执行顺序为preprocess.py -> train.py -> test.py -> postprocess.py。
import oscmd = '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 osfor 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.load_dir, args.model_config, args.checksession, epoch, args.checkpoint, args.pos_r)os.system(cmd)