How to access files inside a Python egg file?

百般思念 提交于 2019-11-29 09:10:01

egg files are zipfiles, so you must access "stuff" inside them with the zipfile module of the Python standard libraries, not with the built-in open function!

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

for example brewer2mpl-1.4.1-py3.6.egg
After Renaming 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)

Madhu Cheemala

Access file from inside egg file

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

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

In 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:

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

just use unzip file.egg this should be enough.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!