Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Glob in python which supports the ** wildcard

Reply
Thread Tools

Glob in python which supports the ** wildcard

 
 
Martin Lundberg
Guest
Posts: n/a
 
      11-22-2010
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

 
Reply With Quote
 
 
 
 
Stefan Sonnenberg-Carstens
Guest
Posts: n/a
 
      11-22-2010
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.

 
Reply With Quote
 
 
 
 
Kurt Mueller
Guest
Posts: n/a
 
      11-22-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.



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
--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Kurt Mueller
Guest
Posts: n/a
 
      11-22-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/2...vely-in-python

Have a nice day
--
(E-Mail Removed)

 
Reply With Quote
 
Martin v. Loewis
Guest
Posts: n/a
 
      11-23-2010
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
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      11-23-2010
On 22 November 2010 21:43, Martin Lundberg <(E-Mail Removed)> wrote:
> 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 **?


This does roughly what you want:

http://code.activestate.com/recipes/...irectory-tree/

--
Cheers,
Simon B.
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      11-23-2010
On 23 November 2010 09:26, Martin Lundberg <(E-Mail Removed)> wrote:
> 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.

--
Cheers,
Simon B.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
glob.glob output Hitesh Python 6 03-13-2007 03:45 PM
glob.glob unicode bug or feature Elbert Lev Python 5 08-02-2004 12:09 AM
Question about glob.glob <--newbie Sean Berry Python 3 05-04-2004 05:34 PM
RE: Bug in glob.glob for files w/o extentions in Windows Tim Peters Python 1 12-01-2003 09:22 AM
Bug in glob.glob for files w/o extentions in Windows Georgy Pruss Python 15 12-01-2003 04:04 AM



Advertisments