[Tutor] How can I make a python script go directory by directory and excecute on files of choice
Ed Singleton
singletoned at gmail.com
Wed Jan 11 14:48:11 CET 2006
On 11/01/06, Liam Clarke <ml.cyresse at gmail.com> wrote:
> Hi Srinivas -
>
> For walking a directory, you can use os.walk() or os.path.walk(), but
> I prefer the path module here -
> http://www.jorendorff.com/articles/python/path/.
The Path module is excellent, but it's walk still doesn't take into
account the depth of the current file in the folder structure.
If you need that, I wrote (with Kent's help) a simple script that will
take it into account (you need the Path module above for it to work).
def traverse(directory, function, depth=0):
import path
thedir = path.path(directory)
for item in thedir.files():
function(item, depth)
for item in thedir.dirs():
traverse(item,function, depth+1)
It can be used like:
def doprint(item, depth):
print item
traverse(r"C:\Temp", doprint)
Hope it's helpful to someone.
Ed
More information about the Tutor
mailing list