Glob in python which supports the ** wildcard

Discussion in 'Python' started by Martin Lundberg, Nov 22, 2010.

  1. 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
    Martin Lundberg, Nov 22, 2010
    #1
    1. Advertising

  2. 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.
    Stefan Sonnenberg-Carstens, Nov 22, 2010
    #2
    1. Advertising

  3. Martin Lundberg

    Kurt Mueller Guest

    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
    --
    Kurt Mueller, Nov 22, 2010
    #3
  4. Martin Lundberg

    Kurt Mueller Guest

    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 Mueller, Nov 22, 2010
    #4
  5. 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
    Martin v. Loewis, Nov 23, 2010
    #5
  6. On 22 November 2010 21:43, Martin Lundberg <> 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/499305-locating-files-throughout-a-directory-tree/

    --
    Cheers,
    Simon B.
    Simon Brunning, Nov 23, 2010
    #6
  7. On 23 November 2010 09:26, Martin Lundberg <> 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.
    Simon Brunning, Nov 23, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georgy Pruss
    Replies:
    15
    Views:
    712
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    348
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    340
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    382
    Neil Hodgson
    Aug 2, 2004
  5. Replies:
    7
    Views:
    815
Loading...

Share This Page