pattern search
Paul McGuire
ptmcg at austin.rr.com
Tue Mar 27 13:58:02 EDT 2007
On Mar 27, 10:18 am, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
> Fabian Braennstroem wrote:
> > Hi,
>
> > I wrote a small gtk file manager, which works pretty well. Until
> > now, I am able to select different file (treeview entries) just by
> > extension (done with 'endswith'). See the little part below:
>
> > self.pathlist1=[ ]
> > self.patternlist=[ ]
> > while iter:
> > # print iter
> > value = model.get_value(iter, 1)
> > # if value is what I'm looking for:
> > if value.endswith("."+ pattern):
> > selection.select_iter(iter)
> > selection.select_path(n)
> > self.pathlist1.append(n)
> > self.patternlist.append(value)
> > iter = model.iter_next(iter)
> > # print value
> > n=n+1
>
> > Now, I would like to improve it by searching for different 'real'
> > patterns just like using 'ls' in bash. E.g. the entry
> > 'car*.pdf' should select all pdf files with a beginning 'car'.
> > Does anyone have an idea, how to do it?
>
> Use regular expressions. They are part of the module "re". And if you use
> them, ditch your code above, and make it just search for a pattern all the
> time. Because the above is just the case of
>
> *.ext
>
> Diez- Hide quoted text -
>
> - Show quoted text -
The glob module is a more direct tool based on the OP's example. The
example he gives works directly with glob. To use re, you'd have to
convert to something like "car.*\.pdf", yes?
(Of course, re offers much more power than simple globbing. Not clear
how much more the OP was looking for.)
-- Paul
More information about the Python-list
mailing list