[Tutor] Python N00bi - Tough Cookie

Guess?!? wtfwhoami at gmail.com
Fri Dec 7 12:42:31 CET 2007

Hello All,

I want trying to write a program that searches all the files (recursively)
under a given directory in the filesystem for phone
 numbers in the form of (626) 674-5901 and  and then outputting all phone
numbers found in a unique format 626-674

(If several numbers have the same office code, there should be only one line
of output for that office code.)

There are following requirments
1> Search for a pattern in all files in a directory
2> Outputing the result with a unique format ( (626) 674-5901 -> 626-674 )
3> if the result has new office code (which I am guessing first 3 digits --
626) -- add to new line
   if the result has office code already in the list then append


I have generated the regular expression for the pattern ....  and have
tested it also....


>>> import re
>>> p = re.compile('\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}')
>>> p = re.compile('\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}')
>>> p
<_sre.SRE_Pattern object at 0x00C400B8>
>>> p
<_sre.SRE_Pattern object at 0x00C400B8>
>>> print p.match("")
>>> print p.match('(619) 223-1212')
<_sre.SRE_Match object at 0x00A3F678>

I need options to proceed after finding the match in the files.....

I was thinking to find all filenames in the directory using something like

import os
path="C:\\somedirectory"  # insert the path to the directory of interest
for fname in dirList:
    print fname

Am I thinking correct ???

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071207/b4fdd313/attachment.htm 

More information about the Tutor mailing list