Search code examples
pythonpathlib

How to iterate over files in specific directories?


I'd like to iterate over files in two folders in a directory only, and ignore any other files/directories. e.g in path: "dirA/subdirA/folder1" and "dirA/subdirA/folder2"

I tried passing both to pathlib as:

root_dir_A = "dirA/subdirA/folder1"
root_dir_B = "dirA/subdirA/folder2"
for file in Path(root_dir_A,root_dir_B).glob('**/*.json'):
    json_data = open(file, encoding="utf8")
    ...

But it only iterates over the 2nd path in Path(root_dir_A,root_dir_B).


Solution

  • You can't pass two separate directories to Path(). You'll need to loop over them.

    for dirpath in (root_dir_A, root_dir_B):
        for file in Path(dirpath).glob('**/*.json'):
             ...
    

    According to the documentation, Path("foo", "bar") should produce "foo/bar"; but it seems to actually use only the second path segment if it is absolute. Either way, it doesn't do what you seemed to hope it would.