determine directories with wildcard

Discussion in 'Python' started by Thomas Rademacher, Mar 8, 2005.

  1. Hello,

    I want to collect with the wildcard '*' all existing directories.
    For example: /dir/dir/*/dir/*/dir/* or C:\dir\dir\*\dir\*\dir\*

    How can I resolve this problem?

    Thanks for your hints, Thomas.
     
    Thomas Rademacher, Mar 8, 2005
    #1
    1. Advertising

  2. On Tuesday 08 March 2005 14:33, Thomas Rademacher wrote:
    > How can I resolve this problem?


    python
    >>> import glob
    >>> help(glob)


    or look at the online documentation for glob.

    --
    --- Heiko.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.0 (GNU/Linux)

    iD8DBQBCLau7f0bpgh6uVAMRAq7EAJ9XgYPfTiwfSCR4AOgKkoAXeb0jRQCbBw/e
    uLCTnWwkK5wansgRM1JsGKU=
    =Gplj
    -----END PGP SIGNATURE-----
     
    Heiko Wundram, Mar 8, 2005
    #2
    1. Advertising

  3. Thomas Rademacher wrote:

    > Hello,
    >
    > I want to collect with the wildcard '*' all existing directories.
    > For example: /dir/dir/*/dir/*/dir/* or C:\dir\dir\*\dir\*\dir\*
    >
    > How can I resolve this problem?


    This is some sort of pattern matching. What I'd do is to convert your
    pattern to a regex:

    rex = re.compile(r"/dir/dir/.*/dir/.*/dir/.*")

    Then create a list of dirs with os.walk:

    dirs = [dirpath for dirpath, foo, bar in os.walk(topdir) if
    rex.match(dirpath)]


    This is untested, but should do the trick.



    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Mar 8, 2005
    #3
  4. Thomas Rademacher wrote:
    > Hello,
    >
    > I want to collect with the wildcard '*' all existing directories.
    > For example: /dir/dir/*/dir/*/dir/* or C:\dir\dir\*\dir\*\dir\*
    >
    > How can I resolve this problem?
    >
    > Thanks for your hints, Thomas.
    >
    >


    You may want to check out the glob module. E.g. something like:

    >> import glob, os
    >> p = "c:/*/*/site-packages/*"
    >> dirs = [d for d in glob.glob(p) if os.path.isdir(d)]
    >> print dirs

    ['c:/Python23\\Lib\\site-packages\\atox',
    'c:/Python23\\Lib\\site-packages\\BDBStorage',
    'c:/Python23\\Lib\\site-packages\\BitTorrent',
    'c:/Python23\\Lib\\site-packages\\BTrees',
    'c:/Python23\\Lib\\site-packages\\ChartDirector',
    'c:/Python23\\Lib\\site-packages\\cjkcodecs',
    'c:/Python23\\Lib\\site-packages\\ctypes',
    'c:/Python23\\Lib\\site-packages\\CVS',
    'c:/Python23\\Lib\\site-packages\\elementtree',
    'c:/Python23\\Lib\\site-packages\\enchant',
    'c:/Python23\\Lib\\site-packages\\Ft',
    'c:/Python23\\Lib\\site-packages\\imdb',
    'c:/Python23\\Lib\\site-packages\\isapi',
    'c:/Python23\\Lib\\site-packages\\logilab',
    'c:/Python23\\Lib\\site-packages\\mx',
    'c:/Python23\\Lib\\site-packages\\MySQLdb',
    'c:/Python23\\Lib\\site-packages\\numarray',
    'c:/Python23\\Lib\\site-packages\\OpenGL',
    'c:/Python23\\Lib\\site-packages\\OpenGLContext',
    'c:/Python23\\Lib\\site-packages\\parallel',
    'c:/Python23\\Lib\\site-packages\\Persistence',
    'c:/Python23\\Lib\\site-packages\\PIL',
    'c:/Python23\\Lib\\site-packages\\psyco',
    'c:/Python23\\Lib\\site-packages\\py2exe',
    'c:/Python23\\Lib\\site-packages\\pychecker',
    'c:/Python23\\Lib\\site-packages\\pynsource',
    'c:/Python23\\Lib\\site-packages\\Pyrex',
    'c:/Python23\\Lib\\site-packages\\Pyro',
    'c:/Python23\\Lib\\site-packages\\pythonwin',
    'c:/Python23\\Lib\\site-packages\\pywin32_system32',
    'c:/Python23\\Lib\\site-packages\\pyXLWriter',
    'c:/Python23\\Lib\\site-packages\\reportlab',
    'c:/Python23\\Lib\\site-packages\\serial',
    'c:/Python23\\Lib\\site-packages\\spambayes',
    'c:/Python23\\Lib\\site-packages\\ThreadedAsync',
    'c:/Python23\\Lib\\site-packages\\vrml',
    'c:/Python23\\Lib\\site-packages\\win32',
    'c:/Python23\\Lib\\site-packages\\win32com',
    'c:/Python23\\Lib\\site-packages\\win32comext',
    'c:/Python23\\Lib\\site-packages\\wx',
    'c:/Python23\\Lib\\site-packages\\wxPython',
    'c:/Python23\\Lib\\site-packages\\ZConfig',
    'c:/Python23\\Lib\\site-packages\\zdaemon',
    'c:/Python23\\Lib\\site-packages\\ZEO',
    'c:/Python23\\Lib\\site-packages\\zLOG',
    'c:/Python23\\Lib\\site-packages\\ZODB',
    'c:/Python23\\Lib\\site-packages\\ZopeUndo',
    'c:/Python23\\Lib\\site-packages\\_xmlplus',
    'c:/Python24\\Lib\\site-packages\\ChartDirector',
    'c:/Python24\\Lib\\site-packages\\elementtidy',
    'c:/Python24\\Lib\\site-packages\\elementtree',
    'c:/Python24\\Lib\\site-packages\\isapi',
    'c:/Python24\\Lib\\site-packages\\py2exe',
    'c:/Python24\\Lib\\site-packages\\pythonwin',
    'c:/Python24\\Lib\\site-packages\\pywin32_system32',
    'c:/Python24\\Lib\\site-packages\\win32',
    'c:/Python24\\Lib\\site-packages\\win32com',
    'c:/Python24\\Lib\\site-packages\\win32comext',
    'c:/Python24\\Lib\\site-packages\\wx-2.5.3-msw-unicode']


    --

    Vincent Wehren
     
    Vincent Wehren, Mar 8, 2005
    #4
  5. On Tue, 8 Mar 2005 14:33:59 +0100, Thomas Rademacher
    <-berlin.de> wrote:
    > Hello,
    >
    > I want to collect with the wildcard '*' all existing directories.
    > For example: /dir/dir/*/dir/*/dir/* or C:\dir\dir\*\dir\*\dir\*


    How about something like:

    import fnmatch
    import os

    root = 'd:\\'
    filter = r'*\*python*\*'

    for dirpath, dirnames, filenames in os.walk(root):
    if fnmatch.fnmatch(dirpath, filter):
    print 'matched', dirpath

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Mar 8, 2005
    #5
  6. Thanks. It works fine! Thomas
    "Vincent Wehren" <> schrieb im Newsbeitrag
    news:d0kabn$93a$1.ov.home.nl...
    > Thomas Rademacher wrote:
    > > Hello,
    > >
    > > I want to collect with the wildcard '*' all existing directories.
    > > For example: /dir/dir/*/dir/*/dir/* or C:\dir\dir\*\dir\*\dir\*
    > >
    > > How can I resolve this problem?
    > >
    > > Thanks for your hints, Thomas.
    > >
    > >

    >
    > You may want to check out the glob module. E.g. something like:
    >
    > >> import glob, os
    > >> p = "c:/*/*/site-packages/*"
    > >> dirs = [d for d in glob.glob(p) if os.path.isdir(d)]
    > >> print dirs

    > ['c:/Python23\\Lib\\site-packages\\atox',
    > 'c:/Python23\\Lib\\site-packages\\BDBStorage',
    > 'c:/Python23\\Lib\\site-packages\\BitTorrent',
    > 'c:/Python23\\Lib\\site-packages\\BTrees',
    > 'c:/Python23\\Lib\\site-packages\\ChartDirector',
    > 'c:/Python23\\Lib\\site-packages\\cjkcodecs',
    > 'c:/Python23\\Lib\\site-packages\\ctypes',
    > 'c:/Python23\\Lib\\site-packages\\CVS',
    > 'c:/Python23\\Lib\\site-packages\\elementtree',
    > 'c:/Python23\\Lib\\site-packages\\enchant',
    > 'c:/Python23\\Lib\\site-packages\\Ft',
    > 'c:/Python23\\Lib\\site-packages\\imdb',
    > 'c:/Python23\\Lib\\site-packages\\isapi',
    > 'c:/Python23\\Lib\\site-packages\\logilab',
    > 'c:/Python23\\Lib\\site-packages\\mx',
    > 'c:/Python23\\Lib\\site-packages\\MySQLdb',
    > 'c:/Python23\\Lib\\site-packages\\numarray',
    > 'c:/Python23\\Lib\\site-packages\\OpenGL',
    > 'c:/Python23\\Lib\\site-packages\\OpenGLContext',
    > 'c:/Python23\\Lib\\site-packages\\parallel',
    > 'c:/Python23\\Lib\\site-packages\\Persistence',
    > 'c:/Python23\\Lib\\site-packages\\PIL',
    > 'c:/Python23\\Lib\\site-packages\\psyco',
    > 'c:/Python23\\Lib\\site-packages\\py2exe',
    > 'c:/Python23\\Lib\\site-packages\\pychecker',
    > 'c:/Python23\\Lib\\site-packages\\pynsource',
    > 'c:/Python23\\Lib\\site-packages\\Pyrex',
    > 'c:/Python23\\Lib\\site-packages\\Pyro',
    > 'c:/Python23\\Lib\\site-packages\\pythonwin',
    > 'c:/Python23\\Lib\\site-packages\\pywin32_system32',
    > 'c:/Python23\\Lib\\site-packages\\pyXLWriter',
    > 'c:/Python23\\Lib\\site-packages\\reportlab',
    > 'c:/Python23\\Lib\\site-packages\\serial',
    > 'c:/Python23\\Lib\\site-packages\\spambayes',
    > 'c:/Python23\\Lib\\site-packages\\ThreadedAsync',
    > 'c:/Python23\\Lib\\site-packages\\vrml',
    > 'c:/Python23\\Lib\\site-packages\\win32',
    > 'c:/Python23\\Lib\\site-packages\\win32com',
    > 'c:/Python23\\Lib\\site-packages\\win32comext',
    > 'c:/Python23\\Lib\\site-packages\\wx',
    > 'c:/Python23\\Lib\\site-packages\\wxPython',
    > 'c:/Python23\\Lib\\site-packages\\ZConfig',
    > 'c:/Python23\\Lib\\site-packages\\zdaemon',
    > 'c:/Python23\\Lib\\site-packages\\ZEO',
    > 'c:/Python23\\Lib\\site-packages\\zLOG',
    > 'c:/Python23\\Lib\\site-packages\\ZODB',
    > 'c:/Python23\\Lib\\site-packages\\ZopeUndo',
    > 'c:/Python23\\Lib\\site-packages\\_xmlplus',
    > 'c:/Python24\\Lib\\site-packages\\ChartDirector',
    > 'c:/Python24\\Lib\\site-packages\\elementtidy',
    > 'c:/Python24\\Lib\\site-packages\\elementtree',
    > 'c:/Python24\\Lib\\site-packages\\isapi',
    > 'c:/Python24\\Lib\\site-packages\\py2exe',
    > 'c:/Python24\\Lib\\site-packages\\pythonwin',
    > 'c:/Python24\\Lib\\site-packages\\pywin32_system32',
    > 'c:/Python24\\Lib\\site-packages\\win32',
    > 'c:/Python24\\Lib\\site-packages\\win32com',
    > 'c:/Python24\\Lib\\site-packages\\win32comext',
    > 'c:/Python24\\Lib\\site-packages\\wx-2.5.3-msw-unicode']
    >
    >
    > --
    >
    > Vincent Wehren
     
    Thomas Rademacher, Mar 8, 2005
    #6
    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. Joel Finkel
    Replies:
    0
    Views:
    505
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    547
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    839
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. Replies:
    7
    Views:
    879
  5. JSkinn3
    Replies:
    3
    Views:
    787
    Rikishi42
    Mar 19, 2011
Loading...

Share This Page