Answer the question
In order to leave comments, you need to log in
How can I create a zipfile.Zipfile in the folder I need?
I do in my application the return of all files from a folder in one archive. Everything is cool, the archive is being created, but for some reason, when downloading, it has no name, and inside there is a full path to files from empty directories. How do I create an archive file in the media/archs/ folder and how do I assign a name to the archive when downloading?
views.py
def DownloadAllFilesView(request, objId):
if request.method == 'POST':
project_folder = Doc.objects.filter(idProj=Project.objects.get(id=objId)).first().idProj.name.replace(' ', '_')
path = settings.MEDIA_ROOT + '/project_%s/' % project_folder
z_name = '%s.zip' % Project.objects.get(id=objId).name.replace(' ', '_')
z = zipfile.ZipFile(z_name, 'w') # Создание нового архива
for file in os.listdir(path):
z.write(os.path.join(path, file)) # Создание относительных путей и запись файлов в архив
z.close()
return FileResponse(open(z.filename, 'rb'))
Answer the question
In order to leave comments, you need to log in
inside there is a full path to files from empty directories.https://docs.python.org/3/library/zipfile.html#zip...
How do I create an archive file in the media/archs/ folderin z_name specify the absolute path to this location.
how to assign a name to an archive when downloadingyou the third time to give a link to everything on the same?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question