Skip to content

filter_dirs in walk.dirs behaves unintuitive and its description is a bit misleading #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dAnjou opened this issue Jan 24, 2020 · 1 comment · Fixed by #464
Closed
Labels
docs Issues related to documentation

Comments

@dAnjou
Copy link

dAnjou commented Jan 24, 2020

Talking about https://docs.pyfilesystem.org/en/latest/reference/walk.html#fs.walk.BoundWalker.dirs and maybe walk.files is affected as well.

The documentation talks about (emphasis by me):

dirs(path='/', **kwargs)
Walk a filesystem, yielding ABSOLUTE paths to directories.

and

filter_dirs (list, optional) – A list of patterns that will be used to match directories PATHS. The walk will only open directories that match at least one of these patterns.

This gives me the impression that the filters are applied to the absolute paths.

While in fact, if I understood and investigated correctly, these patterns match the directory NAMES. Here's an example to show that behavior:

with MemoryFS() as myfs:
    myfs.makedirs('/foo/bar')
    print('-------------------')
    for path in myfs.walk.dirs():
        print(path)
    print('-------------------')
    for path in myfs.walk.dirs(filter_dirs=['*bar*']):
        print(path)
    print('-------------------')

Actual output:

-------------------
/foo
/foo/bar
-------------------
-------------------

Expected output:

-------------------
/foo
/foo/bar
-------------------
/foo/bar
-------------------

This is probably not a bug in the code. However, the documentation is misleading and my question would be how I can get to my desired behavior.

@Blizzke
Copy link

Blizzke commented Aug 11, 2020

Not a reply as to your remark (I agree its a bit limited in use), but I've been banging my head against the same problem.

The only solution I see (to work around it) is to implement a derived Walker class. If it overrides the "check_open_dir" function (or check_scan_dir which is called right after) and returns False, the directory will be skipped.
Both functions receive the path and the Info for the directory about to be opened/scanned.
It's a bit more work but the only one I can see

(You can specify the Walker class to use by using a BoundWalker)

@althonos althonos added the docs Issues related to documentation label Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Issues related to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants