Searching for Files Based on Extension : Path : File PYTHON TUTORIALS


PYTHON TUTORIALS » File » Path »

 

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)



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo File
» Path