How to Search files in code?

Example on how to parse/search for file in a directory

import os
import re

PATH = '.'       # Current Directory
PATTERN = '.bak' # Check for if filename contains this pattern

def print_matched_files(path=PATH,pattern=PATTERN):
    prints files in a location
    count = 1
    print '[=================][Input]'
    print 'File Location',path
    print 'Search pattern provided',pattern
    print '[=================][Printing files ....]'
    files = [f for f in os.listdir(PATH) if re.match(PATTERN, f)]
    for each in files:
        print count,'Filename Name',each
        count = count + 1
    print '[=================][Printing files completed]'

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s