1. 程式人生 > >Python實現批量執行同目錄下的py檔案

Python實現批量執行同目錄下的py檔案

Python版本:3.5

網上找了好多資料都沒有直觀的寫出怎麼批量執行,so,整理了一個小程式。最初是為了用Python進行單元測試,同目錄下有兩個unittest檔案,


AllTest.py的目的是批量執行mytest.py、mytest2.py。程式碼如下:

#批量執行當前資料夾下所有py檔案
import os  
lst=os.listdir(os.getcwd())   

for c in lst:
	if os.path.isfile(c) and c.endswith('.py') and c.find("AllTest")==-1:    #去掉AllTest.py檔案
		print(c)
		os.system(os.path.join(os.getcwd(),c))    #E:\Python\mytest.py