简体   繁体   中英

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.

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_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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM