1、執行test文件夾下的testA和testB
import os path = "/Users/ddc-test/Downloads/pycharm/test" lst = os.listdir(path) for c in lst: if c.endswith('.py') and c.find("__init__") == -1: #方法一 os.chdir(path) #切換目錄 commend = 'python3 {}'.format(c) os.system(commend) #或 os.system('python3 {}'.format(c)) #方法二 commend = 'python3 {}'.format(os.path.join(path,c)) os.system(commend)
2、test_data.txt文件中寫入需要執行的文件名,按名字來批量執行
import os f = open("test_data",'r') data = f.read().split('/') f.close() path = "/Users/ddc-test/Downloads/pycharm/test" lst = os.listdir(path) for i in data: for c in lst: if i in c: os.chdir(path) commend = 'python3 {}'.format(c) os.system(commend)
留空