【发布时间】:2022-01-03 17:01:04
【问题描述】:
在下面的代码中,我试图压缩文件列表,我试图在压缩之前重命名文件。所以文件名将是用户更易读的格式。
第一次运行,但是当我再次运行它失败并显示文件名已存在的错误
通过 FileResponse 通过 Django Rest Framework 返回响应。 有没有更简单的方法来实现这一点?
filenames_list=['10_TEST_Comments_12/03/2021','10_TEST_Posts_04/10/2020','10_TEST_Likes_04/09/2020']
with zipfile.ZipFile(fr"reports/downloads/reports.zip", 'w') as zipF:
for file in filenames_list:
friendly_name = get_friendly_name(file)
if friendly_name is not None:
os.rename(file,fr"/reports/downloads/{friendly_name}")
file = friendly_name
zipF.write(fr"reports/downloads/{file}", file, compress_type=zipfile.ZIP_DEFLATED)
zip_file = open(fr"reports/downloads/reports.zip", 'rb')
response = FileResponse(zip_file)
return response
【问题讨论】:
-
你可能想看看库
tempfile -
请修正缩进。现在你有一堆错误
-
@Mad Physicist 更正了缩进
-
@RossRogers。这不适用于这里
标签: python python-3.x zipfile python-zipfile