lists and files question

hokieghal99 hokiegal99 at hotmail.com
Wed Jul 23 16:29:45 EDT 2003


Thanks to everyone for the feedback on this. I've learned a lot from you 
guys.

John Hunter wrote:
> Others have already answered your question - I just want to point out
> a few of other things
> 
>     import os, re, string
>     setpath = raw_input("Enter the path: ")
>     for root, dirs, files in os.walk(setpath):
>     	id = re.compile('Microsoft Excel Worksheet')
> 
> 1) id is a built in function; you may not want to override it with
>    your variable name
> 
>    >>> x = 1
>    >>> id(x)
>    135313208
> 
> 2) The reason to use re.compile is for efficiency.  There is no need
>    to call it inside the loop, since you're just recompiling the same
>    regex over and over again.  Instead, compile the regex outside the
>    loop
> 
>     >>> rgx = re.compile('[A-Z]+')
>     >>> for some_text in some_list:
>     ...     m = rgx.match(some_text)
> 
> 3) If you want to match 'Microsoft Excel Worksheet', you don't need
>    regular expressions since this is a string literal.  You will
>    probably be better off just using the string find method, as in
> 
>    s.find('Microsoft Excel Worksheet')
> 
> 4) You may want to look at the path module, which provides a nice
>    interface for walking over files:
>    http://www.jorendorff.com/articles/python/path/
> 
>     >>> from path import path
>     >>> xldir = path(setpath)
>     >>> for f in xldir.files('*.xls'):
>     ...     print f.read().find('Microsoft Excel Worksheet')
> 
> Cheers,
> John Hunter
> 





More information about the Python-list mailing list