查找特定后缀的文件

# -*- coding: cp936 -*-
#python 27
#xiaodeng
#查找特定后缀的文件

#方法一:
import os
#os.listdir,获取指定目录下的内容
#返回一个list
#该目录下所有的内容都将被返回

import os
def FileType(fileName,*args):
    for types in args:
        if fileName.endswith(types):return fileName

if __name__ == '__main__':
    listFile = os.listdir('C:python')#对应要查找的文件目录
    HouZhui=('.txt','.py','.html')#定义需要查找的文件后缀,请带上点号,可定义一个后缀,也可以定义多个
    for fileName in listFile:
        if FileType(fileName,HouZhui)!=None:
            print FileType(fileName,HouZhui)

# -*- coding: cp936 -*-
#python 27
#xiaodeng
#查找特定后缀的文件


#方法二
import os
#os.listdir,获取指定目录下的内容
#返回一个list
#该目录下所有的内容都将被返回
listFile = os.listdir('C:python')
for fileName in listFile:
    fileHtml =fileName.endswith('.html')
    fileTxt=fileName.endswith('.txt')
    if fileHtml or fileTxt:
        print fileName
原文地址:https://www.cnblogs.com/dengyg200891/p/4894157.html