#!/usr/bin/env python
# This should work with numbers that look like:
# (111) 111-1111
# (111)111-1111
# and much less well tested with 1111111111 :)
# From here it is a matter of adding recursive directory walking with
# os.walk, see: https://docs.python.org/3/library/os.html#os.walk
# and wrap `with open` context manager in a for loop which takes path
# yielded by os.walk and passes it to context manager where we can handle
# things like failure to open files, etc. Remainder should not require modification.
import re, sys
phre = re.compile(r'''(
(\d{3}|\(\d{3}\))?
(\s|-|\.)?
(\d{3})
(\s|-|\.)
(\d{4})
(\s*(ext|x|ext.)\s*(\d{2,5}))?
)''', re.VERBOSE)
def read(file_obj):
while True:
data = file_obj.readline()
if not data:
break
yield data
def process(line):
s = phre.search(line)
if s:
return line[s.start():s.end()]
return None
if __name__ == "__main__":
if len(sys.argv[:]) < 2:
sys.exit(1)
with open(sys.argv[1], 'rb') as f:
for line in read(f):
result = process(line)
if result:
print(result)