python遍历并输出该文件夹与其子目录下所有后缀为x的文件

path就是需要遍历的父目录
rule为输出遍历文件的后缀
改一下变量名就阔以直接用了

# coding:utf-8
import os

def get_files(path='D:\zyt\\azyt\sfx', rule=".sfx"):
    all = []
    for fpathe,dirs,fs in os.walk(path):   # os.walk获取所有的目录
        for f in fs:
            filename = os.path.join(fpathe,f)
            if filename.endswith(rule):  # 判断是否是".sfx"结尾
                all.append(filename)
    return all

if __name__ == "__main__":
    b = get_files(r"D:\zyt\\azyt\sfx")
    for i in b:
        print i

你可能感兴趣的:(python)