How to import files using REGEX?

0

Good Afternoon Personal, okay?

I have a question in regex in python.

I want to create a script to open files without informing their format, being

In this case, I would like to know the name of the image, without informing its format (informing the format inside the regex). Any solution tips?

    
asked by anonymous 03.09.2018 / 21:24

1 answer

0

You can create a pattern with the desired name, and possible extensions. Assuming you want to search for image.png , or image.jpg or image.gif within c: / test

import re
import os
padrao = r'imagem\.(png|jpg|gif)'
base_dir = 'c:/teste/'
for arquivo in os.listdir():
    if re.match(padrao, arquivo):
        with open(base_dir + arquivo) as reader:
            print(reader.read())
    
03.09.2018 / 22:19