Skip to content
Advertisement

How to temporarily re-name a file or Create a re-named temp-file in Python before zipping it

In the below code I am trying to zip a list list of files, I am trying to rename the files before zipping it. So the file name will be in a more readable format for the user.

It works for the first time, but when I do it again It fails with the error the file name already exists

Returning the response via Django Rest Framework via FileResponse. Is there any more simplistic way to achieve this?

JavaScript

Advertisement

Answer

ZipFile.write has a second parameter, arcname, which allows you to rename files without any copying. You don’t need to move file to a separate folder, or actually rename it.

JavaScript

By stripping off the basename, you avoid the need to move to a common folder at all.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement