Search code examples
pythonregexglob

Python Glob regex file search with for single result from multiple matches


In Python, I am trying to find a specific file in a directory, let's say, 'file3.txt'. The other files in the directory are 'flie1.txt', 'File2.txt', 'file_12.txt', and 'File13.txt'. The number is unique, so I need to search by a user supplied number.

file_num = 3
my_file = glob.glob('C:/Path_to_dir/' + r'[a-zA-Z_]*' + f'{file_num} + '.txt')

Problem is, that returns both 'file3.txt' and 'File13.txt'. If I try lookbehind, I get no files:

file_num = 3
my_file = glob.glob('C:/Path_to_dir/' + r'[a-zA-Z_]*' + r'(?<![1-9]*)' + f'{file_num}' +  '.txt')

How do I only get 'file3.txt'?


Solution

  • glob accepts Unix wildcards, not regexes. Those are less powerful but what you're asking can still be achieved. This:

    glob.glob("/path/to/file/*[!0-9]3.txt")
    

    filters the files containing 3 without digits before.

    For other cases, you can use a list comprehension and regex:

    [x for x in glob.glob("/path/to/file/*") if re.match(some_regex,os.path.basename(x))]