python打包下载多个文件

本文共有926个字,关键词:

问题:如何批量下载文件并打包成压缩包进行下载?

解决:使用python内置模块zipfile

方法:

from zipfile import ZipFile
class DownloadHanlder(BaseHandler):
    zipfile_path = os.path.realpath(
        os.path.join(os.path.dirname(__file__),
                     "../../static/download/Data.zip"))
    newzip = ZipFile(zipfile_path, "w")
    user_uv = {'code':0, 'name': '用户数据.xls', 'file_path': '/static/download/user.xls'}
    if not user_uv['code']:
        newzip.write(self.absolute_path(user_uv['file_path']), user_uv['name'])
    self.write({
        'code': 0,
        'file_path': '/static/download/Data.zip',
        })

def absolute_path(self, relative_path):
    abs_path = os.path.realpath(
        os.path.join(os.path.dirname(__file__),
                     "../../%s" % relative_path))
    return abs_path
版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。
添加新评论
暂无评论