【问题标题】:Store a file insde python file在 python 文件中存储一个文件
【发布时间】:2018-05-05 11:19:48
【问题描述】:
我不知道它叫什么。我看到一个安装程序在安装程序脚本的末尾存储了数据,看起来只是一堆随机文本,然后在运行安装程序时变成一个单独的文件。有一些命令说类似line xxx -> end | file.txt。如果这没有意义,我很抱歉,但如果有人知道我在寻找什么,我喜欢在 python 脚本中做同样的事情。对资源的指示或关于如何在此处进行操作的说明将非常感谢。
【问题讨论】:
标签:
python
python-3.x
file
io
【解决方案1】:
shell 脚本这样做的一个原因是 shell 变量不能包含任意二进制数据(即空字节)。 Python 变量没有这样的限制;只需将数据存储在变量中即可。
【解决方案2】:
看看get-pip.py。
# Create a temporary working directory
tmpdir = tempfile.mkdtemp()
# Unpack the zipfile into the temporary directory
pip_zip = os.path.join(tmpdir, "pip.zip")
with open(pip_zip, "wb") as fp:
fp.write(b85decode(DATA.replace(b"\n", b"")))