determine directories with wildcard

  • Thread starter Thomas Rademacher
  • Start date
T

Thomas Rademacher

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.
 
H

Heiko Wundram

How can I resolve this problem?
python

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-----
 
D

Diez B. Roggisch

Thomas said:
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.
 
V

Vincent Wehren

Thomas said:
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']
 
S

Simon Brunning

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
 
T

Thomas Rademacher

Thanks. It works fine! Thomas
Vincent Wehren said:
Thomas said:
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']
 

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,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top