PyFilesystem / pyfilesystem2

Python's Filesystem abstraction layer

Home Page:https://www.pyfilesystem.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unintuitive behavior of walk filter_dirs

JustASquid opened this issue · comments

Check out this example:

import fs
import tempfile
import os

from fs.osfs import OSFS

with tempfile.TemporaryDirectory() as test_dir:
    with open(os.path.join(test_dir, "1.txt"), "wt") as f:
        f.write("text")
    os.mkdir(os.path.join(test_dir, "dir"))
    with open(os.path.join(test_dir, "dir", "2.txt"), "wt") as f:
        f.write("text")

    my_fs = OSFS(test_dir)
    for path in my_fs.walk.files(filter_dirs=["/dir"]):
        print(path)

I would expect the output to be

/dir/2.txt

But it is actually

/1.txt

Seems like there are 2 issues here:

  1. The matcher does not match "dir" with a leading "/" in this case. And indeed, if I remove the leading "/", I get both files in the output.
  2. Files in the root directory are not filtered out. I would expect only files in dir to be output.