一个巨基础的问题:Python 获取所有子目录

刚刚在 stack overflow 上看到的答案,以前一直用的 filter。。。没注意到有这么优雅的做法,还是要学习一个

[x[0] for x in os.walk(directory)]

用法举例:

获取全部以txt结尾的文件

import os
from glob import glob
result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.txt'))]

 

Leave a Reply

Your email address will not be published. Required fields are marked *