写这篇博客的原因:python
面试归来翻脉脉发现一个陌生的朋友提出一个面试题,设计实现遍历目录及子目录,抓取.pyc文件。面试
并贴出两种实现方法:编程
我的感受,这两种方法中规中矩,不像是python的风格。python风格的实现能够考虑python列表推导式。spa
下面贴出个人实现方法:设计
1 def getFiles(path, suffix): 2 return [os.path.join(root, file) for root, dirs, files in os.walk(path) for file in files if file.endswith(suffix)]
我的的信条:编程简单粗暴才是真理。code