|
|
 |
|
Title: Locating files throughout a directory tree
Submitter: Simon Brunning
(other recipes)
Last Updated: 2008/03/14
Version no: 1.1
Category:
Files
|
|
|
Description:
os.walk is a very nice replacement for os.path.walk, which I never did feel comfortable with. There's one very common pattern of usage, though, which still benefits from a simple helper-function; locating all files matching a given file-name pattern within a directory tree.
Source: Text Source
import os, fnmatch
def locate(pattern, root=os.curdir):
'''Locate all files matching supplied filename pattern in and below
supplied root directory.'''
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
Discussion:
An example - I needed to identify all malformed XML files within my project. With the help of locate (and ElementTree) all I needed was:
from xml.parsers.expat import ExpatError
import xml.etree.ElementTree as ElementTree
for xml in locate("*.xml"):
try:
ElementTree.parse(xml)
except (SyntaxError, ExpatError):
print xml, "\tBADLY FORMED!"
Thanks to Marius Gedminas and Tom Lynn for improvements incorporated in this code.
|
|
Add comment
|
|
Number of comments: 7
a simple question, Lucas Schifferle, 2006/12/12
How could the script be modified so that it locates all files in the current (or specified) root directory only - not its sub-directories?
Thanks.
Add comment
os.listdir(), Rafal Zawadzki, 2006/12/12
import os
os.listdir()
Add comment
naturally i mean listdir as a keyword only, Rafal Zawadzki, 2006/12/12
import os, fnmatch
def locate(pattern, root=os.curdir):
'''Locate all files matching supplied filename pattern in and below
supplied root directory.'''
#for path, dirs, files in os.walk(os.path.abspath(root)):
files = os.listdir(os.path.abspath(root))
for filename in fnmatch.filter(files, pattern):
yield filename
Add comment
a simple solution (?), Lucas Schifferle, 2006/12/12
Thanks for the help!
A solution to locate files matching a pattern inside the root dir only (so not its sub-dirs) could be:
def locate2(pattern, root=os.curdir):
filename = fnmatch.filter(os.listdir(os.path.abspath(root)), pattern)
for fn in filename:
yield os.path.join(root, fn)
Add comment
writing at the same time..., Lucas Schifferle, 2006/12/12
Thanks again for the help!
Add comment
Locating files in a single directory, Simon Brunning, 2007/01/10
The glob module in Python's standard library will find files matching a pattern in a single directory. Check out the docs.
Add comment
glob example, Martin Laloux,Martin Laloux, 2008/03/15
import os, glob
dor = the path you want
for dir, subdir, files in os.walk(dor):
for file in files:
if glob.fnmatch.fnmatch(file,"*.txt"):
do what you want
Add comment
|
|
|
|
|
 |
|