glob() that traverses a folder tree

S

seannakasone

i'm looking for something like glob.glob() that traverses
sub-directories. is there anything like that? i guess i'm looking for
something to replace the unix find command.
 
S

seannakasone

# i'm guessing os.walk() is the best way to traverse folder trees.

import os, glob

for dir, subdir, files in os.walk('.\InteropSolution'):
for file in files:
if glob.fnmatch.fnmatch(file,"*.dll") or
glob.fnmatch.fnmatch(file,"*.exe"):
print dir+file
 
K

Kent Johnson

# i'm guessing os.walk() is the best way to traverse folder trees.

import os, glob

for dir, subdir, files in os.walk('.\InteropSolution'):
for file in files:
if glob.fnmatch.fnmatch(file,"*.dll") or
glob.fnmatch.fnmatch(file,"*.exe"):
print dir+file

Or use Jason Orendorff's path module. For a single glob it is very easy:

import path
for f in path.path('.\InteropSolution').walkfiles('*.dll'):
print f

For multiple globs you have to work a little harder:
for f in path.path('.\InteropSolution').walkfiles():
if f.fnmatch('*.dll') or f.fnmatch('*.exe'):
print f

or maybe
for f in path.path('.\InteropSolution').walkfiles():
if f.ext in ['.dll', '.exe']:
print f

http://www.jorendorff.com/articles/python/path/index.html

Kent
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top