[Tutor] os.path.walk vs unix find command
kent37 at tds.net
Sat Aug 16 05:13:33 CEST 2008
On Fri, Aug 15, 2008 at 6:17 PM, Angela Yang <angelayian at yahoo.com> wrote:
> Hi Python gurus:
> Is os.popen("find") faster or slower than os.path.walk to find file pattern
> in the
> directory tree? I thought os.path.walk would be faster than unix find, but
> that doesn't
> seem to be the case?
In general a mature (i.e. optimized) C program is going to be faster
than a Python program to do the same task.
> What is the fastest way in python to search for a file with a given pattern
> in a directory
> Here's the code using os.path.walk:
os.path.walk() is more-or-less obsolete, os.walk() does the same job
and is much easier to use. I don't know if it is any faster though.
If I understand your code, you want to find every file named src.mk
that is in a directory named src. With os.walk() it would look like
for dirpath, dirnames, filenames in os.walk(component_dir):
if 'src.mk' in filenames:
# or just handle the file here, no need to make the list
More information about the Tutor