如何安全地访问与 python 模块捆绑的数据文件?
假设我的 python 包中包含相同的数据文件。
据我所知,distils 不建议使用 __file__
,因为如果您的模块位于 Egg 文件内,您将无法获得有效路径。
现在,我应该使用哪种方法才能读取这些文件(在鸡蛋内部还是外部)?
Let's say that I do have same data files that are included in my python package.
I understand that the usage of __file__
is not recommended by distils because you will not be able to get a valid path if your module is inside an egg file.
Now, which method should I use in oder to be able to read these files, being inside eggs or outside them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用评论中链接的其他问题,我得到了一个非常适合 Python 2.6+ 的解决方案。特别是,在我自己的模块中,我同时拥有代码和数据文件,如下所示:
为了访问 code.py 中的 data.dat,我这样做了:
Using the other question linked in the comments, I got a solution that works pretty well for Python 2.6+. In particular, in my own module, I have both code and data files like so:
To get access to data.dat in code.py, I did this: