Search code examples
pythonglob

glob "one or more" in Python


I need to get all text files with numeric names: 1.txt, 2.txt, 13.txt
Is it possible to do with glob?

import glob

for file in glob.glob('[0-9].txt'):
    print(file)

Does not return 13.txt.
And there seems to be no regex's one or more + operator.

What can I do?


Solution

  • I don't think that glob() is meant to be that customisable. You might want to try os.listdir() instead:

    import os,re
    for f in os.listdir("/path/to/dir"):
        if re.match(r"^\d+\.txt$", f):
            print(f)