我正在使用一个 Flask 应用程序,该应用程序从 Web 服务器的文件夹中获取多个文件,并创建一个包含这些文件的 zip 文件,最终将该 zip 文件发送给用户。我的问题是我无法在发送后从 Web 服务器中删除 zip 文件。
我尝试使用@after_this_request命令解决问题。
在这里,我展示了我编写的代码的简化版本。我发送了之前创建的 zip 文件,然后尝试删除它。
@app.route('/sendFile')
def sendFile():
@after_this_request
def removeFile(response):
os.remove(zip_path)
return response
return send_from_directory(path, filename=zipname, as_attachment=True)
Run Code Online (Sandbox Code Playgroud)
问题是引发了一个错误,指出无法删除 zip 文件,因为它仍在被其他进程使用。我在其他类型的文件中看到过类似的问题:
但是,在我使用 zip 文件的情况下,它们不起作用。