简体   繁体   English

如何在django中将zipfile作为File对象?

[英]How to take a zipfile as a File object in django?

I have a folder named 'src' of which I create a zip file named zf. 我有一个名为“ src”的文件夹,其中创建了一个名为zf的zip文件。 I want to pass zf as a FileField object to newobj.fd. 我想将zf作为FileField对象传递给newobj.fd。

                import zipfile
                from django.core.files import File

                f='s1.zip'
                zf = zipfile.ZipFile(f, "w")
                for dirname, subdirs, files in os.walk(src):
                    zf.write(dirname)
                    for filename in files:
                        zf.write(os.path.join(dirname, filename))
                        zf.open(os.path.join(dirname, filename))
                newobj.fd= File(zf)

I do this thing for a text file and it works: 我对一个文本文件执行此操作,它可以正常工作:

                f=file('text.txt')
                newobj.fd2=File(f)
                f.close()

How do the same thing for a zipfile? zipfile怎么做?

            import zipfile
            from django.core.files import File

            f='s1.zip'
            zf = zipfile.ZipFile(f, "w")
            for dirname, subdirs, files in os.walk(src):
                zf.write(dirname)
                for filename in files:
                    zf.write(os.path.join(dirname, filename))
            zf.close()
            newobj.fd = File(f)

What does this give? 这有什么作用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM