0

I am trying to list only specified files in my directory but getting only empty results. When I checked the expected results with regex tester (https://regex101.com/), I've got the expected results.

enter image description here

But in the python code, I can see nothing.

I tried some other combinations in regex string but nothing helped. I can list the whole directory though.

Am I doing something wrong ? It looks like quite simple solution so what am I missing ?

Code here:

import os
import fnmatch

directory = 'output/'

for filename in os.listdir(directory):
    if fnmatch.fnmatch(filename, '^[20]+[0-9]+\.xlsx$'):
        print(filename)

Can you please have a look and give me some hint ?

Many thanks

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
BedSon
  • 47
  • 1
  • 7

1 Answers1

0

OK guys, thanks Wiktor for your reply. After that I look for a workaround and I came with this.

x = re.search("^[20]+[0-9]+\.xlsx$", filename)
            if x is not None:
                print(filename)

I am getting satisfying result so I think, I can close this one.

Many thaanks.

BedSon
  • 47
  • 1
  • 7