递归实现查找path目录下的所有文件
def filenames(path, list_names):
if os.path.isdir(path):
subnames = os.listdir(path)
for subname in subnames:
filenames(path + '/' + subname, list_names)
else:
list_names.append(path)
调用
if __name__ == '__main__':
list_name = []
path = 'G:\\4分类数据'
filenames(path, list_name)
for name in list_name:
print(name)
结果
G:\4分类数据/4分类/7间期+R幅值/9-7间期+R幅值.CSV
G:\4分类数据/4分类/7间期+R幅值/9.CSV
G:\4分类数据/4分类/7间期+R幅值/新建 DOCX 文档.docx
G:\4分类数据/4分类/7间期+R幅值/间期+幅值.docx
G:\4分类数据/4分类/amp/Rwave/k=5.docx