‘‘‘
需求:获取指定目录下及其子目录下的有.py文件
‘‘‘
import os
l=[]
def getPyFile(path,l):
fileList = os.listdir(path) #获取文件及文件夹
for filename in fileList:
filepath = os.path.join(path,filename) #路径拼接
if os.path.isdir(filepath):
getPyFile(filepath,l)
else:
# filename = os.path.splitext(filepath) #获取文件的扩展名
# print(filename)
if filename[-3:] == ".py":
l.append(filepath)
if __name__ == "__main__":
p = input("请输入路径:")
getPyFile(p,l)
for fileItem in l:
print(fileItem)
原文:https://www.cnblogs.com/nishoufeng/p/12063872.html