Search code examples
pythonglob

Use glob to find arbitrary length numbers


I'm looking for the glob-pattern that find all files containing the pattern myfile_[SomeNumber].txt

My naive attempt was

glob.glob("myfile_[0-9]*.txt")

but this also find all files on the form myfile_[SomeNumber][AnyStuff].txt

This answer shows how to do it for a fixed length, but that not what I want in this case. use python glob to find a folder that is a 14 digit number


Solution

  • You are probably confusing regular expression syntax with glob constructs. [0-9]* in globbing means "a single digit followed by zero or more of any character". So drop the *.

    In extended globbing there is a qualifier of "one or more", but that is not supported by glob, so there is little choice but to use a regular expression, i.e. do your own filename pattern matching. There are several ways to do this, here is one:

    import os
    import re
    
    files = []
    for fname in os.listdir('.'):
        if re.match(r"myfile_[0-9]+.txt", fname):
            files.append(fname)
    
    print files
    

    Note that the RE is not exactly the same as yours, I use + which means "one of more of the preceding pattern", an * would mean "zero or more" - so the digits would be optional, which could be what you want (I'm not sure).

    The bulk of the code could be done as a list comprehension, but that would arguably loose some readability:

    files = [fname for fname in os.listdir('.') 
            if re.match(r"myfile_[0-9]+.txt", fname)]