获取指定路径下特定后缀的文件

# 获取指定路径下所有指定后缀的文件
# dir 指定路径
# ext 指定后缀,链表&不需要带点 或者不指定。例子:['xml', 'java']
import os
def GetFileFromThisRootDir(dir,ext = None):
allfiles = []
needExtFilter = (ext != None)
for root,dirs,files in os.walk(dir):
for filespath in files:
filepath = os.path.join(root, filespath)
extension = os.path.splitext(filepath)[1][1:]
if needExtFilter and extension in ext:
allfiles.append(filepath)
elif not needExtFilter:
allfiles.append(filepath)
return allfiles

GetFileFromThisRootDir('C:\data\1','.txt')

原文地址:https://www.cnblogs.com/wyj690/p/5386971.html