import os
lst = os.listdir(os.getcwd()) # 获取当前目录下所有的文件名
for c in lst:
if os.path.isfile(c) and c.endswith('.py') and c.find("run")== -1: #判断文件名是以.py结尾的,并且去掉run.py文件
print(c) #查看文件
os.system('python {}'.format(c)) #相当于在终端执行文件 python main.py
用这段代码的问题就是,第一个0.py文件运行后,因为是一个死循环的程序,所以后面的文件一直不能运行,而且后面的文件也是死循环的文件。
可以参考这个,在另一终端中运行程序,不知道能不能解决问题。
或者使用subprocess模块,参考这个链接,能够实现主程序不等待子程序运行结束。
import os
import subprocess
lst = os.listdir(os.getcwd()) # 获取当前目录下所有的文件名
childs = []
for c in lst:
if os.path.isfile(c) and c.endswith('.py') and c.find("run")== -1:
print(c) #查看文件
# os.system('python {}'.format(c))
childs.append(subprocess.Popen('python {}'.format(c),shell=True))
for child in childs:
printf("Finished")
child.wait()
可以编写一个程序,以os.system外部调用的方式调用你这几个py,可以实现