网上找了一段代码学习
#获得目录下所有文件名
import os
# 要检查的文件类型
dict_filetype = ["cpp", "c", "h","py"]
# brief : 递归获得所有文件名
# param : path 起始目录,要检查的根目录
# param : allfile 填空即可
# return: 列表 所有与 dict_filetype 对应的文件名
def get_filename(path, allfile):
filelist = os.listdir(path)
for filename in filelist:
filepath = os.path.join(path, filename)
# 判断文件夹
if os.path.isdir(filepath):
# 文件夹继续递归
get_filename(filepath, allfile)
else:
# 文件,判断文件类型
for filetype in dict_filetype:
temp_file_type = filepath.split(".")
#add by huangbq: 等同于 temp_file_type[-1]
if filetype == temp_file_type[-1:][0]:
allfile.append(filepath)
break
# 展示所有非code文件
else:
print("the file is not code : %s" %filepath )