'Capture only exact word with regex in python [duplicate]
I want to capture with regex all the files in a folder that contain for example the string "key1" but I don't want to capture the files that have another character after that.
Let's say in the folder there are three files:
- key1.txt
- key12.txt
- xykey1.txt
When I execute my code, I would like only the first and third file to show.
This is the code I have:
import os
import re
dest = "C:\Work\Python\Files"
for root, dirs, files in os.walk(dest):
for file in files:
if re.findall("key1", file, re.IGNORECASE):
print(file)
With my current code, all three files are caught.
Can someone give me a hint on how not to get "key12" but only the files which contain "key1"?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
