找了很多资料没有明确的说明,为什么system()不能批量执行py程序?
因为在执行的情况中,单个文件执行起来没有问题,如果多个,要么会报错,要么就只执行最后一个命令。
最重要的情况,很多人想着去指定路径,再去遍历,可就是不会执行。
下面直接上代码,让程序返回路径,让程序按照返回的并且认可的路径去遍历,最后再按照程序识别的路径去执行。
1 import os 2 3 D_list = os.listdir(os.getcwd()) 4 5 for file_name in D_list: 6 if os.path.isfile(file_name) and file_name.endswith(‘.py‘) and file_name.find("test1") == -1: # 去掉test1.py文件 7 print(file_name) 8 os.system(os.path.join(os.getcwd(), file_name))
这个代码放置在所有执行文件的同级路径中
原文:https://www.cnblogs.com/meipu/p/12981917.html