tags:

views:

29

answers:

1

Hi, I am trying to find all the files in the same directory as my script that has a filename matching a certain pattern. Ideally, I would like to store it in an array once I get them. The pattern I need to match is something like: testing.JUNK.08-05.txt. All the filenames have the testing in the front and end with the date (08-05.txt). The only difference is the JUNK in the middle which can include any valid characters.

What would be the most efficient way to do this? I can be working with anywhere from 1 to thousands of files?

Additional things to note: Using python 2.6 and I need this to work on Unix-based operating systems.

+5  A: 

Use the glob module:

import glob
for name in glob.glob('testing*08-05.txt'):
    print name
Lukáš Lalinský
Actually, `testing.*.08-05.txt`.
KennyTM
Perfect thank you.
bobby_mansono