您的位置:首页 > 编程语言 > Python开发

Python - 多次检查后缀名(endwith)

2015-07-26 08:33 519 查看
在通过后缀名查找类型文件的时候, 多次使用endwith, 使用元组(tuple), 简化操作.

此类方式, 也可以应用于if语句多次相似检测.

代码

# 列出文件夹内所有代码
def list_dictionary_codes(root_dir):
paths_list = []
for parent, dirNames, fileNames in os.walk(root_dir):
for name in fileNames:
ext = ['.h', '.m', '.xib', '.json', '.c', '.cpp', '.mm', '.md']
if name.endswith(tuple(ext)):
paths_list.append(os.path.join(parent, name))
return paths_list
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Mystra Python 多次 endwith