Using glob (was Re: Remove files of same type (extension))

Peter Hansen peter at engcorp.com
Tue Sep 25 00:26:14 EDT 2001


Marcin 'Qrczak' Kowalczyk wrote:
> 
> Mon, 24 Sep 2001 17:38:28 +0400, A.A <andtherewaswater at yahoo.com> pisze:
> 
> > You can use the fnmatch module to match using dos/unix like patterns:
> 
> Or the glob module.

I used to think glob was a good choice, and simpler than os.listdir
plus os.path.walk etc because it would handle things like */*.txt and
returned the full path.

I just got caught, however, by my apparent lack of understanding of
the phrase "according to the rules used by the Unix shell" from 
the library reference info on glob.

glob.glob('*') returns all files *except* those that start with a period

glob.glob('.*') returns all files which do start with a period.

To use glob to get *all* files, one must do something like

  glob.glob('*') + glob.glob('.*')

I'd be happy to learn otherwise, but that's what reading the 
source confirmed for me...

Still easier than os.listdir sometimes, but I wonder if it should
be considered rather specialized.  Or was that already the general
consensus?

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list