[Tutor] newbie question about grep in Python

Eric Culpepper eculpepper at hcc-care.com
Wed Feb 11 10:59:10 EST 2004



-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org]On
Behalf Of Karl Pflästerer
Sent: Tuesday, February 10, 2004 6:25 PM
To: tutor at python.org
Subject: Re: [Tutor] newbie question about grep in Python

Thanks for the replies everyone! I really appreciate the direction


>You know that sometimes a shell script is the best tool for such jobs so
>Python won't show all its beauty there.

I totally agree with this, the Company is wanting to take these scripts and take them cross-platform, the thought was at first to use Cygwin on the Windows platforms and leaving the scripts as is, but now Management is wanting the scripts to do more and more so Python started to look as a possible solution for a cross-platform scripting/programming language. 

I took your script and changed it slightly to parse the entire directory in one go, granted I'm still learning this, but I'm getting the output I wanted and can move forward now.

Here's my version of the script incase anyone can suggest further improvements
-------------------------------
#!/usr/local/bin/python

import os, re

path = "/shc1/p"
reg = re.compile('#V#\$Date:(.*)#V#')

for root,dirs,files in os.walk(path):
   for name in files:
      ffile = path + "/" + name
      strings = open(ffile,'r').read()
      m = reg.search(strings)
      if m:
         print ffile, "Date:", re.sub('[a-zA-Z$]', '', m.groups()[0]) 


Thanks again!
Eric Culpepper





More information about the Tutor mailing list