Sam Henry
Sam Henry

Reputation: 314

How to walk a resource directory in a Python package?

I have a Python package with a resource folder being bundled into the build. The structure is similar to the following:

package
  - resources
    - subfolder
      - resource1.txt
    - resource2.txt

I know about importlib_resources and the standard version of that package, but I need an equivalent of os.walk for the resource directory (it could be heavily nested). Is there any way to do this? The files() and contents() methods don't seem to be able to achieve this.

Upvotes: 2

Views: 845

Answers (2)

Sam
Sam

Reputation: 339

Sam Henry's answer is deprecated according to setuptools. The new way for python 3.9+ is importlib.resources.files(package), which is traversable.

Upvotes: 0

Sam Henry
Sam Henry

Reputation: 314

After more research, I believe that at its current state, importlib_resources is not an options for this task. It has no way to traverse down directories. It may be available in a future release.

The easiest solution (albeit slow) is pkg_resources from the package setuptools. This package is not meant to be a run-time library, but for these purposes, we will make it one.

Here is a snippet for walking a package data directory:

import pkg_resources

RESOURCE_ROOT = "mypackage"

def walk_data(base_dir: str, path: str = ""):
    for name in pkg_resources.resource_listdir(RESOURCE_ROOT, f"{base_dir}{path}"):
        path = f"{base_dir}{path}/{name}"
        if pkg_resources.resource_isdir(RESOURCE_ROOT, path):
            walk_data(base_dir, f"{path}/{name}")
        else:
            content = pkg_resources.resource_string(RESOURCE_ROOT, path)
            # Do something with the file content

Upvotes: 1

Related Questions