[Tutor] Python N00bi - Tough Cookie

Kent Johnson kent37 at tds.net
Fri Dec 7 15:32:43 CET 2007


Guess?!? wrote:
> Hello All,
>  
> I want trying to write a program that searches all the files ( 
> recursively) under a given directory in the filesystem

> 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....
>  
> \([0-9]{3}\)\s[0-9]{3}-[0-9]{4}
>  
>> >> 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("")
> None
>> >> print p.match('(619) 223-1212')
> <_sre.SRE_Match object at 0x00A3F678>
>  
> I need options to proceed after finding the match in the files.....

You can replace text using p.sub() (re.sub)

> ++++++++++++++++++++++++++++++++
> 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
> here
> dirList=os.listdir(path)
> for fname in dirList:
>     print fname
>  
> Am I thinking correct ???

Yes, did you try this code? It looks good to me.

To open the file you have to construct the full path, e.g.
fpath = os.path.join(path, fname)
f = open(fpath)
data = f.read()
f.close()

Then you can process the data with your regex and write it back out.

Kent


More information about the Tutor mailing list