bobby_mansono
bobby_mansono

Reputation: 93

How to find all files in current directory with filenames that match a certain pattern in python?

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.

Upvotes: 9

Views: 5425

Answers (1)

Lukáš Lalinský
Lukáš Lalinský

Reputation: 41306

Use the glob module:

import glob
for name in glob.glob('testing*08-05.txt'):
    print name

Upvotes: 15

Related Questions