首页 > 解决方案 > 在 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

要下载的文件是在下载之前生成的。
这个时候,是否可以实现下载完成后删除文件呢?

标签: pythondjangopython-3.xdjango-rest-framework

解决方案


推荐阅读