0

I'm trying to delete .zip file from server directory right after client has downloaded it, but everytime I'm trying to execute 'os.remove' there's an error: "file is used by another process etc etc etc"

This code is inside 'app.route' method

    if (buttonName == "downloadFactures"):
      with ZipFile('sampleDir.zip', 'w') as zipObj:
        for facturesData in notApprovedFactures:
          zipObj.write(facturesData[2])

        return send_from_directory(app.root_path, "sampleDir.zip", as_attachment=True)


    else:
     -- nothing important

I tried to do something like:

zipObj = ZipFile(blabla)
forLoop
  zipObj.write

zipObj.close()
zipDownload = send_from_directory
os.remove(here's path for zipFile in server directory)
return zipDownload

but it didn't work, obviously it didn't work because file is still being used by client which is downloading this zip.

So here's my question if anyone know, how can I remove that created zipFile from my server directory after client download it?

Kravvczyk
  • 1
  • 1
  • in the first code, make sure that `return send_from_directory` is _outside_ the with block so zipfile can be closed. – Jean-François Fabre Mar 02 '20 at 20:40
  • I was testing several ways to do that, and nothing, because the minimum zipfile size is ~50mb that's why I can't remove that file, because it is still being used by client, he is downloading that file... I don't know exactly when his browser has finished downloading – Kravvczyk Mar 02 '20 at 20:43
  • you could use `io.Bytes()` to create zip file in memory without writing on disk. – furas Mar 02 '20 at 20:44

0 Answers0