Using File Extension Wildcards in os.listdir(path)

Maybe the glob module can help you:

import glob

listing = glob.glob('C:/foo/bar/foo.log*')
for filename in listing:
    # do stuff

What's the best route to take in order to read and parse only the foo.log.* and foo.log files? The bar.log files do not need to be read.

Your code does this:

if files.endswith('.log'):

You've just translated your English description into Python a bit wrong. What you write in Python is: "read and parse only the *.log files", meaning bar.log is included, and foo.log.1 is not.

But if you think for a second, you can translate your English description directly into Python:

if files == 'foo.log' or files.startswith('foo.log.'):

And if you think about it, as long as there are no files named foo.log. (with that extra dot) that you want to skip, you can collapse the two cases into one:

if files.startswith('foo.log'):

However, if you know anything about POSIX shells, foo.log* matches exactly the same thing. (That's not true for Windows shells, where wildcards treat extensions specially, which is why you have to type *.* instead of *.) And Python comes with a module that does POSIX-style wildcards, even on Windows, called glob. See stranac's answer for how to use this.

I think the glob answer is better than manually filtering listdir. It's simpler, it's a more direct match for what your question title says you want to do (just do exactly what you hoped would work with os.listdir, but with glob.glob instead), and it's more flexible. So, unless you're worried about getting confused by the two slightly different meanings of wildcards, I'd suggest accepting that instead of this one.


This'll give you bash-like regexes:

import glob
print(glob.glob("/tmp/o*"))

Alternatively, you could os.listdir the entire directory, and throw away files that don't match a regex via the re module.