在App Engine中压缩动态文件(Python) [英] Zipping dynamic files in App Engine (Python)

查看:123
本文介绍了在App Engine中压缩动态文件(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有,我可以使用zipfile将动态生成的内容(例如新鲜渲染的html模板)压缩成zip文件?



似乎有一些示例压缩静态内容,但没有压缩动态的内容。或者,这是不可能的吗?

还有一个问题:是否可以创建一个带有一堆子文件夹的zip文件?



谢谢。

解决方案

您可以使用<一个href =http://docs.python.org/library/zipfile#zipfile.ZipFile.writestr =noreferrer> ZipFile.writestr()

  my_data =< html>< body>< p> Hello,world!< / p> ;< /体>< / HTML>中
z.writestr(hello.html,my_data)

您也可以使用sub - 使用 / (或 os.sep )作为分隔符的文件夹:

  z.writestr(site / foo / hello / index.html,my_data)


Is there anyway I can zip dynamically generated content, such as a freshly rendered html template, into a zip file using zipfile?

There seem to be some examples around for zipping static content, but none for zipping dynamic ones. Or, is it not possible at all?

One more question: Is it possible to create a zip file with a bunch of sub-folders inside it?

Thanks.

解决方案

You can add whatever you want to a zip file using ZipFile.writestr():

my_data = "<html><body><p>Hello, world!</p></body></html>"
z.writestr("hello.html", my_data)

You can also use sub-folders using / (or os.sep) as a separator:

z.writestr("site/foo/hello/index.html", my_data)

这篇关于在App Engine中压缩动态文件(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆