Glob in python which supports the ** wildcard

M

Martin Lundberg

Hi,

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 **?

Regards,

Martin Lundberg
 
S

Stefan Sonnenberg-Carstens

Am 22.11.2010 22:43, schrieb Martin Lundberg:
Hi,

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 **?

Regards,

Martin Lundberg
os.walk() or os.path.walk() can be used.
You need to traverse the file system.
AFAIK there is no support for this.
 
K

Kurt Mueller

Hi,


Am 22.11.2010 um 23:05 schrieb Stefan Sonnenberg-Carstens:
Am 22.11.2010 22:43, schrieb Martin Lundberg:
os.walk() or os.path.walk() can be used.
You need to traverse the file system.
AFAIK there is no support for this.


If you are a lucky Unix/Linux/MacOS user:
---------------------------------------------------------------------------
#!/usr/bin/env python
# find files
import os
cmd = 'find apps/name/ -type f -name "*.js" -print' # find is a standard Unix tool
for filename in os.popen(cmd).readlines(): # run find command
# do something with filename
---------------------------------------------------------------------------

find is very powerful, really.


Have anice day
 
K

Kurt Mueller

HI,


Am 22.11.2010 um 23:05 schrieb Stefan Sonnenberg-Carstens:
Am 22.11.2010 22:43, schrieb Martin Lundberg;
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
 
M

Martin v. Loewis

Am 22.11.2010 22:43, schrieb Martin Lundberg:
Hi,

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 **?

mercurial.match supports apps/name/**.js (IIUC).
mglob supports rec:*.js.

Regards,
Martin
 
S

Simon Brunning

It does not seem to support the ** wildcard? It will recursively seek
for files matching a pattern like *.js but it won't support
/var/name/**/*.js as root, will it?

I did say roughly. ;-) You'd need to do:

for filename in locate("*.js", "/var/name/"):
print filename

Adding support for ** is left as an exercise for the reader.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top