Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django中打开zip文件中存储的照片时出错_Python_Django_Django Views_Django 2.2 - Fatal编程技术网

Python 在Django中打开zip文件中存储的照片时出错

Python 在Django中打开zip文件中存储的照片时出错,python,django,django-views,django-2.2,Python,Django,Django Views,Django 2.2,我将从存储在服务器上的一些图像文件创建一个zip文件。 我使用了以下函数来执行此操作: def create_zip_file(user, examination): from lms.models import StudentAnswer f = BytesIO() zip = zipfile.ZipFile(f, 'w') this_student_answer = StudentAnswer.objects.filter(student_id=user.id

我将从存储在服务器上的一些图像文件创建一个zip文件。 我使用了以下函数来执行此操作:

def create_zip_file(user, examination):
    from lms.models import StudentAnswer
    f = BytesIO()
    zip = zipfile.ZipFile(f, 'w')
    this_student_answer = StudentAnswer.objects.filter(student_id=user.id, exam=examination)

    for answer in this_student_answer:
        if answer.answer_file:
            answer_file_full_path = answer.answer_file.path
            fdir, fname = os.path.split(answer_file_full_path)
            zip.writestr(fname, answer_file_full_path)
    zip.close()  # Close
    zip_file_name = "student-answers_"+ str(examination.id)+"_" + str(user.id) + "_" + date=datetime.datetime.now().strftime("%Y-%m-%d-%H-%M") + '.zip'
    response = HttpResponse(f.getvalue(), content_type="application/x-zip-compressed")
    response['Content-Disposition'] = 'attachment; filename=%s' % zip_file_name
    return response
一切都很好,所有的照片都是在zip文件,但只有一个问题。 问题是照片无法打开,此错误将出现在Windows中:

看起来我们不支持这种文件格式。


我的代码有什么问题?

要从文件中附加数据,必须使用


使用
writestr(filename)
只添加变量
filename
中的字符串,而不添加文件中的字符串。

代码似乎可以,但可能您的系统没有zip文件的解压缩程序。请阅读有关zipfile中使用的不同格式的文档。那么我现在该怎么办@furasI多年来都不使用
Windows
,但当我使用Windows时,我总是免费安装,或最终安装,但它不是免费的。我测试了
7-zip
PeaZip
,但结果与错误相同。您确定我在zip文件中创建新的
jpg
时没有任何错误吗@福拉斯