Friday, August 1, 2008

A simple find in python ?

Continuing on my saga with my Python importer for xls, I ran into a situation where I had to dig in recursively into a directory structure and do something with Python. There seemed to be nothing easy in Python such as a 'find . -name '*.xls' -exec perform_task {} \;'. All of the solutions or suggestions that I have run across so far from stuff found in google seems to need me to perform some sort of mental or code acrobats. This is not to create a flame, I am just an intermediate in this so I am welcoming suggestions to do this if you people have any.

8 comments:

Paddy3118 said...

This might help. :-)

- Paddy.

Kent Johnson said...

Using JOrendorff's path module it is as simple as

from path import path
d = path('.')
for f in d.walkfiles('*.xls'):
----# do something with f

http://pypi.python.org/pypi/path.py/2.2

Michael said...

Hi, I recently had the same overall problem but solved it nicely with os.walk(). I had to search a file system for subversion repositories so that they could be backed up. The os.walk() method made very quick work of this.

cowmix said...

I have to 2nd using "JOrendorff's path module"... Its the FIRST thing I install when I get to a new Python setup.. BTW.. it works from Python 2.2 and up.

rgz said...

I too install path.py as soon as I get a chance. path.py is delicious cake.

lowks said...

Thanks for the comments so far. The path module seems good. However, why isn't it part of Python's standard libraries?

lowks said...

Also, the site to download path.py seems to be down

Ορέστης said...

http://pypi.python.org/pypi/grin

Search there for "grind"