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)
留空