Search code examples
pythonglob

Regular expression, glob, Python


I have a folder, contains many files. There is a group contains pc_0.txt,pc_1.txt,...,pc_699.txt. I want to select all files beetween pc_200 - > to pc_699.txt

How?

for filename in glob.glob("pc*.txt"):
    global_list.append(filename)

Solution

  • For this specific case, glob already supports what you need (see fnmatch docs for glob wildcards). You can just do:

    for filename in glob.glob("pc[23456]??.txt"):
    

    If you need to be extra specific that the two trailing characters are numbers (some files might have non-numeric characters there), you can replace the ?s with [0123456789], but otherwise, I find the ? a little less distracting.

    In a more complicated scenario, you might be forced to resort to regular expressions, and you could do so here with:

    import re
    
    for filename in filter(re.compile(r'^pc_[2-6]\d\d\.txt$').match, os.listdir('.')):
    

    but given that glob-style wildcards work well enough, you don't need to break out the big guns just yet.