Monday, September 29, 2025
HomeLanguagesPython program to find files having a particular extension using RegEx

Python program to find files having a particular extension using RegEx

Prerequisite: Regular Expression in Python

Many of the times we need to search for a particular type of file from a list of different types of files. And we can do so with only a few lines of code using python. And the cool part is we don’t need to install any external package, python has a built-in package called re, with which we can easily write the program for performing this task.

Approach:

  • This program searches for the files having “.xml” extension from a list of different files.
  • Make a regular expression/pattern : “\.xml$”
  • Here re.search() function is used to check for a match anywhere in the string (name of the file). It basically returns the match object when the pattern is found and if the pattern is not found it returns null.
  • The functionality of different Metacharacters used here:
  1.  \  It is used to specify a special meaning of character after it. It is also used to escape special characters.
  2.  $ The string ends with the pattern which is before it.
  • Here “.xml” pattern is searched and processed.
  • Below is the implementation:

    Python3




    # import library
    import re
      
    # list of different types of file
    filenames = ["gfg.html", "Lazyroar.xml"
                "computer.txt", "neveropen.jpg"]
      
    for file in filenames:
        # search given pattern in the line 
        match = re.search("\.xml$", file)
      
        # if match is found
        if match:
            print("The file ending with .xml is:",
                 file)

    
    

    Output:

    The file ending with .xml is: Lazyroar.xml
    RELATED ARTICLES

    Most Popular

    Dominic
    32324 POSTS0 COMMENTS
    Milvus
    84 POSTS0 COMMENTS
    Nango Kala
    6695 POSTS0 COMMENTS
    Nicole Veronica
    11860 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11918 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6807 POSTS0 COMMENTS
    Ted Musemwa
    7073 POSTS0 COMMENTS
    Thapelo Manthata
    6763 POSTS0 COMMENTS
    Umr Jansen
    6771 POSTS0 COMMENTS