In Python, you can efficiently identify files with a specific extension from a list of different file types using the built-in re module. This allows you to perform pattern matching without installing any external libraries. This article demonstrates how to find files with a particular extension (for example .xml) using regex.
Approach:
1. Define the regex pattern: To match .xml files, use the pattern: \.xml$
- \. -> Escapes the dot, since '.' has a special meaning in regex.
- xml -> Matches the literal characters xml.
- $ -> Ensures the pattern occurs at the end of the string.
2. Use re.search(): The re.search() function checks for a match anywhere in the string and returns a match object if found, otherwise, it returns None.
3. Loop through the files: Check each file name against the regex pattern and print the files that match the pattern.
Implementation
import re
fn = ["gfg.html", "geeks.xml", "computer.txt", "geeksforgeeks.jpg"]
for file in fn:
match = re.search(r"\.xml$", file)
if match:
print("The file ending with .xml is:", file)
Output
The file ending with .xml is: geeks.xml
Explanation:
- fn = ["gfg.html", "geeks.xml", "computer.txt", "geeksforgeeks.jpg"]: Creates a list of file names with different extensions.
- for file in fn: Loops through each file name in the list.
- match = re.search(r"\.xml$", file): Uses regex to check if the file name ends with .xml.
\. -> Escapes the dot.
xml -> Matches the literal characters xml.
$ -> Ensures .xml is at the end of the string.
Note: This method works for any file extension by simply changing the regex pattern.