9

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

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Mikael Fremling
  • 720
  • 2
  • 8
  • 24

1 Answers1

10

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)]
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • 1
    This would also match a file named "myfile_123.txt.exe", since re.match accepts matches at the beginning of the string. It's better to use re.fullmatch instead. – LHLaurini Sep 12 '19 at 16:42