自动执行文件夹中的py文件

写一个函数,接收一个地址,执行其中的py文件,包括子文件。
path.endswith('.py') 判断以‘.py’结尾,是什么类型的文件。
os.system('python %s'%path) 模拟cmd中执行代码的过程。
一。递归方法:
import os
def func(path):
if os.path.isfile(path) and path.endswith('.py'):
os.system('python %s'%path)
elif os.path.isdir(path):
for name in os.listdir(path):
abs_path=os.path.join(path,name)
func(abs_path)
func(r'D:untitled1')
二。循环方法:
import os
def func(path):
if os.path.isfile(path) and path.endswith('.py'):
os.system('python %s'%path)
elif os.path.isdir(path):
for name in os.listdir(path):
abs_path=os.path.join(path,name)
if abs_path.endswith('.py'):
os.system('python %s'%abs_path)
func(r'D:untitled1')
原文地址:https://www.cnblogs.com/qqq789001/p/13363608.html