473,574 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

glob() that traverses a folder tree

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.

May 11 '06 #1
3 2167
# i'm guessing os.walk() is the best way to traverse folder trees.

import os, glob

for dir, subdir, files in os.walk('.\Inte ropSolution'):
for file in files:
if glob.fnmatch.fn match(file,"*.d ll") or
glob.fnmatch.fn match(file,"*.e xe"):
print dir+file

May 11 '06 #2
se**********@ya hoo.com wrote:
# i'm guessing os.walk() is the best way to traverse folder trees.

import os, glob

for dir, subdir, files in os.walk('.\Inte ropSolution'):
for file in files:
if glob.fnmatch.fn match(file,"*.d ll") or
glob.fnmatch.fn match(file,"*.e xe"):
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('.\In teropSolution') .walkfiles('*.d ll'):
print f

For multiple globs you have to work a little harder:
for f in path.path('.\In teropSolution') .walkfiles():
if f.fnmatch('*.dl l') or f.fnmatch('*.ex e'):
print f

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


May 11 '06 #3
awesome. thanks.

May 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Georgy Pruss | last post by:
On Windows XP glob.glob doesn't work properly for files without extensions. E.g. C:\Temp contains 4 files: 2 with extensions, 2 without. C:\Temp>dir /b * aaaaa.aaa bbbbb.bbb ccccc ddddd C:\Temp>dir /b *.
by: Python Dunce | last post by:
I've run into an issue with glob and matching filenames with brackets '' in them. The problem comes when I'm using part of such a filename as the path I'm passing to glob. Here's a trimmed down dumb example. Let's say I have a directory with the following files in it. foo.par2 foo.vol0+1.par2 foo.vol1+1.par2 zzz .par2 zzz .vol0+1.par2
by: scott | last post by:
I'm creating a tree menu that uses FSO to read folders, subfolders and files. I'd like for each folder to have capabilty of being either Level 1,2,3,4 or 5 security. The 5 Levels are my security levels with 5 being Admin. I'm looking for any advice on how to go about this. I was thinking of creating a hidden text file within each folder and...
by: John H. | last post by:
In effort to understand (Outlook) MAPI folder tree structure wrote simple linear code below to navigate tree. Successive "...Folders.GetNext()"'s return same folder at all levels of tree. What am I doing wrong or not understanding? Thanks.
by: Hitesh | last post by:
import string import os f = open ("c:\\servername.txt", 'r') linelist = f.read() lineLog = string.split(linelist, '\n') lineLog = lineLog #print lineLog for l in lineLog:
by: PythonNewbie | last post by:
Hello, so I needed to write a quick code that lets me merge whole bunch of text files in a given folder into one text file. So, I wrote this up after reading about glob import glob #########Enter the directory where your files are located ####### dirpath ='C:/All_Spot_Spectra/Spots/*'...
by: J | last post by:
Greetings Group- I'm trying to put together a pattern matching script that scans a directory tree for tif images contained in similar folder names, but running into a NewB problem already. Is it the way I'm trying to join multiple paths? Any help would be greatly appericated. Thanks, J! import glob, sys, os topdir = sys.argv
by: billiejoex | last post by:
os.listdir(path) return a list of file names independently if the path- argument used is absolute or relative: '/home/user' glob.glob, instead, return file names only if given path is relative: '/home/user/Desktop
by: Franz Marksteiner | last post by:
Hi folks, is there a way to negate a glob() pattern? What I want is to get a tree structure, e.g. all .php files *and* all directories. How would I start here? -- Freundliche Grüße, Franz Marksteiner
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.