[Python-Dev] Coding guidelines for os.walk filter

Jacek Pliszka jacek.pliszka at gmail.com
Wed Aug 31 00:35:37 CEST 2011


Hi!

I would like to get some opinion on possible os.walk improvement.
For the sake of simplicity let's assume I would like to skip all .svn
and tmp directories.

Current solution looks like this:

for t in os.walk(somedir):
    t[1][:]=set(t[1])-{'.svn','tmp'}
    ... do something

This is a very clever hack but... it relies on internal implementation
of os.walk....

Alternative is adding os.walk parameter e.g. like this:

def walk(top, topdown=True, onerror=None, followlinks=False, walkfilter=None)
....
    if walkfilter is not None:
        dirs,nondirs=walkfilter(top,dirs,nondirs)
.....
and remove .svn and tmp in the walkfilter definition.

What I do not like here is that followlinks is redundant - easily
implementable through walkfilter


Simpler but braking backward-compatibility option would be:

def walk(top, topdown=True, onerror=None, skipdirs=islink)
...
-        if followlinks or not islink(new_path):
-            for x in walk(new_path, topdown, onerror, followlinks):
+        if not skipdirs(new_path):
+            for x in walk(new_path, topdown, onerror, skipdirs):

And user given skipdirs function should return true for new_path
ending in .svn or tmp

Nothing is redundant and works fine with topdown=False!

What do you think?  Shall we:
a) do nothing and use the implicit hack
b) make the option explicit with backward compatibility but with
redundancy and topdown=False incompatibility
c) make the option explicit braking backward compatibility but no redundancy

Best Regards,

Jacek Pliszka


More information about the Python-Dev mailing list