'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. I want to pass zf as a FileField object to 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?
Solution 1:[1]
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dharman |