Finding a files based on search pattern


Within a python script I'd like to find files based on an inputted search pattern (e.g 'test*.txt') and place the files into an array, for subsequent processing.

Any thoughts on how to do this?

Who is Participating?
RichieHindleConnect With a Mentor Commented:
Here you go:
import sys, glob
# Collect the matching filenames here:
filenames = []
# Read one or more search patterns from the command line
for pattern in sys.argv[1:]:
    # For each pattern, expand it into a list of filenames.
    matches = glob.glob(pattern)
    # Add these matches to the list of all the matching files.
# We now have our list of filenames.
print filenames
# Example usage and output:
# > cd \temp
# > python *.java test*
# ['', '', 'test.mbox', 'test.xls', 'TestApp.exe']

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.