Mega Code Archive

 
Categories / Python Tutorial / File
 

Searching for Files Based on Extension

import os path = "/books/python" pattern = "*.py;*.doc" def printFiles(dirList, spaceCount, typeList):     for file in dirList:         for ext in typeList:             if file.endswith(ext):                 print "/".rjust(spaceCount+1) + file                 break def printDirectory(dirEntry, typeList):     print dirEntry[0] + "/"     printFiles(dirEntry[2], len(dirEntry[0]), typeList) extList = [] for ext in pattern.split(";"):     extList.append(ext.lstrip("*")) for directory in os.walk(path):     printDirectory(directory, extList)