patternpythonModerate
Python traverse a directory recursively and print contact numbers
Viewed 0 times
contactdirectorynumberstraverserecursivelyprintpythonand
Problem
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
Please suggest me a better solution than this.
Contact_finder.py
import os, re
class ContactFinder:
pattern = re.compile('^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d
Please suggest me a better solution than this.)
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.
Solution
You don't need a class here at all. You only seem to use it as a namespace and for that just importing it from another file would be sufficient.
I would make
I would put the extracting of the phone numbers into a separate function for readability.
I would make both functions generators and make it the responsibility of the caller to actually print the contact (or do something else with it).
Lastly, I would use a
I would make
pattern a global constant and give it a better name.I would put the extracting of the phone numbers into a separate function for readability.
I would make both functions generators and make it the responsibility of the caller to actually print the contact (or do something else with it).
Lastly, I would use a
if __name__ == "__main__": guard to allow importing these functions without executing the traversal.import os, re class
PHONE_NUNBER = re.compile('^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d)
def get_contacts(filename, pattern):
with open(filename) as fn:
for contact in fn:
if pattern.match(contact):
yield contact
def get_contacts_recursive(dir_path, pattern):
for root, dirs, files in os.walk(dir_path):
for file in files:
filename = os.path.join(root, file)
for contact in get_contacts(filename, pattern):
yield contact
def main():
for contact in get_contacts_recursive('/home/sinscary/Documents/niki', PHONE_NUMBER):
print contact
if __name__ == "__main__":
main()Code Snippets
import os, re class
PHONE_NUNBER = re.compile('^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$')
def get_contacts(filename, pattern):
with open(filename) as fn:
for contact in fn:
if pattern.match(contact):
yield contact
def get_contacts_recursive(dir_path, pattern):
for root, dirs, files in os.walk(dir_path):
for file in files:
filename = os.path.join(root, file)
for contact in get_contacts(filename, pattern):
yield contact
def main():
for contact in get_contacts_recursive('/home/sinscary/Documents/niki', PHONE_NUMBER):
print contact
if __name__ == "__main__":
main()Context
StackExchange Code Review Q#153280, answer score: 10
Revisions (0)
No revisions yet.