python - 在 Python x Django 上完成文件下载后删除文件
问题描述
我可以使用以下代码(Python x Django)下载文件。
file = open(file_path, 'w')
file.write(text)
file.close()
if os.path.exists(file_path):
with open(file_path, 'rb') as fsock:
filename = urllib.parse.quote(filename)
response = HttpResponse()
response['content_type'] = 'text/plain'
response['Content-Disposition'] = "attachment; filename='{}'; filename*=UTF-8''{}".format(filename,filename)
response.write(fsock.read())
return response
要下载的文件是在下载之前生成的。
这个时候,是否可以实现下载完成后删除文件呢?
解决方案
推荐阅读
- typescript - 尝试在 React 组件中向 Kendo Grid 添加简单过滤器时出错
- python-3.x - 用于使用 Python 提取电报数据的 Selenium Web 驱动程序
- go - 无法从 gofiber 会话中的会话存储中获取值
- excel - 如何在没有 VBA 的情况下对 Excel 中的嵌套 if 语句求和?
- r - 如何使用 auth0 R 包访问用户的角色?
- postgresql - 为什么在从 psql 连接到使用 ssl 保护的 postgres 服务器时不要求我们导入 server.crt
- flutter - 未处理的异常:类型“_TypeError”不是“字符串”类型的子类型
- database - 具有 Access 的新数据库架构
- python - Python:在多个 Excel 工作表中搜索关键术语,返回另一个单元格
- python - 将列表中的所有字符串转换为日期格式