wrapped around the axle on regexpressions and file searching
Gordon McMillan
gmcm at hypernet.com
Sat Apr 24 11:21:03 EDT 1999
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
More information about the Python-list
mailing list