Glob in python which supports the ** wildcard

Kurt Mueller kurt.alfred.mueller at sunrise.ch
Tue Nov 23 00:04:08 CET 2010


HI,


Am 22.11.2010 um 23:05 schrieb Stefan Sonnenberg-Carstens:
> Am 22.11.2010 22:43, schrieb Martin Lundberg;
>> 
>> I want to be able to let the user enter paths like this:
>> apps/name/**/*.js
>> and then find all the matching files in apps/name and all its
>> subdirectories. However I found out that Python's glob function
>> doesn't support the recursive ** wildcard. Is there any 3rd party glob
>> function which do support **?
>> 
> os.walk() or os.path.walk() can be used.
> You need to traverse the file system.
> AFAIK there is no support for this.


Or python only:
----------------------------------------------------------
#!/usr/bin/env python
import os, fnmatch
# generator:
def find_files(directory, pattern):
    for root, dirs, files in os.walk(directory):
        for basename in files:
            if fnmatch.fnmatch(basename, pattern):
                filename = os.path.join(root, basename)
                yield filename
# process each file as it is found:
for filename in find_files('apps/name', '*.js'):
    print 'found java source:', filename
----------------------------------------------------------
Found at
http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python

Have a nice day
-- 
kurt.alfred.mueller at gmail.com




More information about the Python-list mailing list