Python资源文件管理

问题描述 投票:0回答:1

我想知道在Python中访问资源文件的好方法。现在我正在使用pkgutil.get_data(...),一旦安装了包,它就可以正常工作,但是在我的开发环境中没有,因为我有'src'和'resources'目录作为兄弟。我还将test_resources保存在另一个兄弟目录中,并希望测试能够找到test_resources。有没有办法扩展pkgutil的搜索路径?我的文件结构示例如下:

├── resources
│   └── download
│       └── file
│           └── type
│               └── config.yaml
├── src
│   └── download
│       ├── __init__.py
│       ├── file
│       │   ├── __init__.py
│       │   ├── type
│       │   │   ├── __init__.py
│       │   │   └── code.py
├── test
│   ├── test_x.py
│   ├── test_y.py
│   └── test_z.py
└── test_resources
    ├── test_resources1.yaml
    └── test_resources2.yaml
python python-3.x config directory-structure
1个回答
0
投票

尝试将这些路径传递给pkgutil调用。

ResrcConfigPath = os.path.expanduser('~path/to/resources/download/file/type/config.yaml')

TestResourcesDir = os.path.expanduser('~path/to/test_resources/')
testResource1Path = os.path.join(TestResourcesDir, "test_resources1.yaml")
testResource2Path = os.path.join(TestResourcesDir, "test_resources2.yaml")
© www.soinside.com 2019 - 2024. All rights reserved.