How to find all files in current directory with filenames that match a certain pattern in python?
I 开发者_JAVA技巧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.
Use the glob
module:
import glob
for name in glob.glob('testing*08-05.txt'):
print name
精彩评论