在Python模块分发中计算目录路径

时间:2016-09-17 00:22:21

标签: python module pkg-resources

我有一个Python模块发行版(它是一个鸡蛋),其中包含一些额外的文件&我希望在程序运行时复制的目录。

├── mypkg
│   ├── extra_files
│   │      ├── layouts/
│   │      ├── scripts/
│   │      └── config.yaml

当使用mypkg时,我想将extra_files目录的所有内容复制到磁盘上的其他位置。类似的东西:

shutil.copytree('mypkg/extra_files', destination)

我已经看了pkg_resources,但似乎我无法计算目录的路径。

如何计算分发包中文件和目录的路径?我是否需要首先列出所有目录,然后在每个目录中的每个文件上调用pkg_resources.resource_stream

1 个答案:

答案 0 :(得分:0)

我能够用__file__实际执行此操作,它返回首次加载模块的路径名。我可以像这样获得静态目录的完整路径:

import os
directory = os.path.join(os.path.dirname(__file__), 'templates/index.html')