如何访问 Python egg 文件中的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3655352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:07:02  来源:igfitidea点击:

How to access files inside a Python egg file?

pythonegg

提问by Ripley

This might be a weird requirement but it's what I've run into. I Googled but yield nothing.

这可能是一个奇怪的要求,但这是我遇到的。我用谷歌搜索但一无所获。

I'm coding an application who's using a lot of constant attributes / values recorded in an XML file (they'll not change so a static file), things work fine until I generated an egg file for it.

我正在编写一个应用程序,该应用程序使用了许多记录在 XML 文件中的常量属性/值(它们不会更改为静态文件),在我为它生成一个egg 文件之前,一切正常。

When the logic reaches the XML accessing part, I got one complaint like this: /home/Workspace/my_proj/dist/mps-1.2.0_M2-py2.6.egg/mps/par/client/syntax/syntax.xml

当逻辑到达 XML 访问部分时,我收到了这样的抱怨:/home/Workspace/my_proj/dist/mps-1.2.0_M2-py2.6.egg/mps/par/client/syntax/syntax.xml

Actually I've bundled the XML file in the path above but seems Python doesn't know how to access it.

实际上我已经在上面的路径中捆绑了 XML 文件,但似乎 Python 不知道如何访问它。

The code to access the XML is as...

访问 XML 的代码是...

file_handler = open(path_to_the_file)
lines = file_handler.read().splitlines()

Any idea?

任何的想法?

采纳答案by Alex Martelli

eggfiles are zipfiles, so you must access "stuff" inside them with the zipfilemodule of the Python standard libraries, notwith the built-in openfunction!

egg文件是zipfile,因此您必须使用Python 标准库的zipfile模块访问其中的“东西” ,而不是使用内置open函数!

回答by H S Rathore

If you want to access the contents inside the .egg file you can simply rename it and change extension from .egg to .zip and than unzip it. Which will create a folder and the contents will be same as they were when it was a .egg file

如果您想访问 .egg 文件中的内容,您可以简单地重命名它并将扩展名从 .egg 更改为 .zip,然后解压缩它。这将创建一个文件夹,内容将与 .egg 文件时相同

for example brewer2mpl-1.4.1-py3.6.egg
After Renaming brewer2mpl-1.4.1-py3.6.zip

例如 brewer2mpl-1.4.1-py3.6.egg
重命名brewer2mpl-1.4.1-py3.6.zip 后

Now if we open it, it'll get easily unzipped and the content will be put in a folder with same name in the same directory. (tested on macOS Sierra)

现在,如果我们打开它,它会很容易解压缩,内容将放在同一目录下的同名文件夹中。(在 macOS Sierra 上测试)

回答by Jorge Machado

just use unzip file.egg this should be enough.

只需使用解压缩 file.egg 这应该就足够了。

回答by Madhu Cheemala

Access file from inside egg file

从egg文件中访问文件

Yes, It is possible to read the files from inside egg file.

是的,可以从egg文件中读取文件。

Egg file: mps-1.2.0_M2-py2.6.eggstructure for module level example:

Egg 文件:mps-1.2.0_M2-py2.6.egg模块级结构示例:

mps-1.2.0_M2-py2.6.egg image

mps-1.2.0_M2-py2.6.egg 图像

In driverfile.py:

driverfile.py

import xml.etree.ElementTree
import mps.par.client as syntaxpath
import os
path = os.path.dirname(syntaxpath.__file__)
element = xml.etree.ElementTree.parse(path+'\syntax\syntax.xml').getroot()
print(element)

Read xml file from inside an eggfile:

从eggfile中读取xml文件:

PYTHONPATH=mps-1.2.0_M2-py2.6.egg python driverfile.py

PYTHONPATH=mps-1.2.0_M2-py2.6.egg python driverfile.py

回答by Rocky Chen

i think by default eggs packing file under python won't add your xml inside to the pack

我认为默认情况下,python 下的鸡蛋打包文件不会将您的 xml 添加到包中