From: gmcm at hypernet.com (Gordon McMillan)
Date: Sat, 24 Apr 1999 15:21:03 GMT
Subject: wrapped around the axle on regexpressions and file searching
In-Reply-To: <7frhe0$8g1$1@nnrp1.dejanews.com>
References: <7frhe0$8g1$1@nnrp1.dejanews.com>
Message-ID: <1287155028-45807148@hypernet.com>
Content-Length: 1723
X-UID: 1494                                                  

msrisney writes:
> 
> I'm attemtping to locate log files on my drive(s), and do some
> comparison. here's a simplified snippet where I'm getting
> bottlenecked.
> 
> >>>import os,re
> >>>regexp = re.compile('.log')
> >>>def find_log_files(arg, directory, names):
>  ...for name in os.listdir(directory):
>         if regexp.search(name):
>            print directory + "\\" + name
> >>>os.path.walk('D:\\',find_log_files,None)
> 
> 
> here are my questions: 1. this prints  out not only files with the
> file extensions ".log" but also any file name that has "log" in it's
> name. how would I rewrite to avoid??

In a regex, a "." is a wildcard character. If you want a literal "." 
you need to escape it: 
 re.compile('\\.log')
or
 re.compile(r'\.log')
 
> 2. is there a better, faster way of doing this??, my end goal is to
> open the files and compare time sequences to one another.

Any number of ways that would be faster. Since you've already got 
os imported, you could use os.path.splitext()
    if os.path.splitext(name)[1] == '.log':

> 3. Is ther any way to determine the number of drives on a system,
> obviuosly I am hardcoding the top level drive letter "D:\", is there
> any way to search the entire system much like win32's find file
> search??

Do you know how to search multiple drives with FindxxxFIle()? I've 
always thought that you either got the current drive or specified the 
drive to search.

At any rate, os.path.exists('e:/') is an effective way of finding out 
if e: exists. Though specifying 'a:/' will pop up one of those lovely 
Abort/Retry/Fail dialogs if nothing is in the drive.

in-nearly-20-years-I-still-haven't-figured-out-the-difference-between
 -Abort-and-Fail-ly y'rs

- Gordon




