0

Hope you are all doing well.

I am currently using fnmatch to match for file names against patterns.

I have not had any issues with simple patterns involving *,?,[seq] as mentioned in the fnmatch documentation.

However I am not able to figure out a way to include other options such as +([0-9]).txt or [0-9]{6}.txt

I will maybe explain what I am trying to achieve. Assume we have 4 files in our source path.

123456.txt (No. of digits will vary)
123_test.txt
test_123.txt
123_test_456.txt

What pattern should be given in fnmatch that matches the file 123456.txt alone while ignoring the other patterns.

import fnmatch
from pathlib import Path
source_file_path = "/files/tst_files/"
source_file_pattern = "*.txt" # 4 Matches - All 4 four files
# source_file_pattern = "*[0-9].txt" # 3 Matches - 123_test_456.txt, 123456.txt, test_123.txt
# source_file_pattern = "[0-9]*.txt" # No Matches
# source_file_pattern = "\b\d+\.txt\b" # No Matches
files = Path(source_file_path).glob('*')

for file in files:
    if fnmatch.fnmatch(file, source_file_pattern):
        print(file)

Env : Python 3.8

Thanks for all the help in advance.

rainingdistros
  • 450
  • 3
  • 11

1 Answers1

0

You have to escape the dot to match it literally, and looking at the example data, you can use word boundaries:

\b\d+\.txt\b

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Thank you for your response... Will this option work inside fnmatch ? let me give it a try... – rainingdistros Jul 28 '22 at 16:50
  • Thank you once again for your response. But unfortunately it did not work. The regex is correct as shown in the link you had shared, just not sure if fnmatch module takes this format. I have updated the sample code in the question. – rainingdistros Jul 29 '22 at 03:23
  • @rainingdistros Did you try writing it as `r'\b\d+\.txt\b'` or `"\\b\\d+\\.txt\\b"` – The fourth bird Jul 29 '22 at 05:44
  • Thank you for your response. I have tried both the options, but both of them are not working. Updated code in the question. As mentioned earlier, the regex you have provided is correct and working, just not working within the `fnmatch` which expects `unix shell style` wildcard patterns as shown in the link - [fnmatch](https://docs.python.org/3/library/fnmatch.html) – rainingdistros Jul 29 '22 at 06:28