I am writing a python code to recursively traverse a directory containing only text files and then print the 10 digit Indian phone number and all its variants. I am very new to python and I have written the following code to print the numbers and this code is working fine. I want to know if there is any other better way to do this program so that the code looks better. Right now, I have used 3 nested for loops, I was wondering if I could reduce time complexity a bit. I am a beginner in python.
Contact_finder.py
import os, re
class ContactFinder:
pattern = re.compile('^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$')
def traverse(self,dir_path):
for root, dirs, files in os.walk(dir_path):
for file in files:
filename = os.path.join(root, file)
with open(filename) as fn:
for contact in fn:
if self.pattern.match(contact):
print contact
contact = ContactFinder()
contact.traverse('/home/sinscary/Documents/niki')
Please suggest me a better solution than this.