title: Remove file after Flask serves it
body: <p>I have a Flask view that generates data and saves it as a CSV file with Pandas, then displays the data. A second view serves the generated file. I want to remove the file after it is downloaded. My current code raises a permission error, maybe because <code>after_request</code> deletes the file before it is served with <code>send_from_directory</code>. How can I delete a file after serving it?</p>
  
  <pre><code>def process_data(data)
      tempname = str(uuid4()) + '.csv'
      data['text'].to_csv('samo/static/temp/{}'.format(tempname))
      return file
  
  @projects.route('/getcsv/&lt;file&gt;')
  def getcsv(file):
      @after_this_request
      def cleanup(response):
          os.remove('samo/static/temp/' + file)
          return response
  
      return send_from_directory(directory=cwd + '/samo/static/temp/', filename=file, as_attachment=True)
  </code></pre>