This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: allow filters in os.walk
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.3
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Jacek.Pliszka, eric.araujo, rhettinger
Priority: normal Keywords: patch

Created on 2011-08-20 11:52 by Jacek.Pliszka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
os.diff Jacek.Pliszka, 2011-08-20 11:52 diff for Python 2.7
Messages (3)
msg142523 - (view) Author: Jacek Pliszka (Jacek.Pliszka) Date: 2011-08-20 11:52
I suggest a small change in os.walk module.  

Instead of:
def walk(top, topdown=True, onerror=None, followlinks=False):

I would like to have:
def walk(top, topdown=True, onerror=None, skipnames=lambda x : False, skipdirs=islink):

Implementation might be as follows:

<         if isdir(join(top, name)):
>         fullname=join(top, name)
>         if skipnames(fullname):
>             continue
>         if isdir(fullname):


<         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, skipnames, skipdirs):

This is a small change, breaks a bit 'followlinks' option but gives
much more flexibility as skipnames and skidirs can be any 
functions (including ones using regexp and similar).
msg142616 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-08-21 12:17
Thanks for the report.  2.7 is a stable version, so this would have to go in 3.3.  Even where, breaking the function signature wouldn’t be possible, so we would have to add arguments without removing any.

Have you looked at shutil.rmtree’s ignore argument and the shutil.ignore_patterns factory function?  Maybe that would be a good pattern to copy.
msg143275 - (view) Author: Jacek Pliszka (Jacek.Pliszka) Date: 2011-08-31 18:34
Looks like the proper way to do it is described in the manual:

for root, dirs, files in os.walk('python/Lib/email'):
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories

I checked that it is covered by unit tests in / so it is safe to use and bug can blo closed as invalid.
Date User Action Args
2022-04-11 14:57:20adminsetgithub: 57002
2011-08-31 19:50:56rhettingersetstatus: open -> closed
2011-08-31 18:34:32Jacek.Pliszkasetresolution: not a bug
messages: + msg143275
2011-08-21 12:17:52eric.araujosetnosy: + eric.araujo

messages: + msg142616
versions: + Python 3.3, - Python 2.7
2011-08-20 12:07:58rhettingersetassignee: rhettinger

nosy: + rhettinger
2011-08-20 11:53:19Jacek.Pliszkasetversions: + Python 2.7, - Python 3.2
2011-08-20 11:52:53Jacek.Pliszkacreate