Answer the question
In order to leave comments, you need to log in
How to delete files after n minutes in Python?
On the server, I generate a file at the request of the user to give it to the browser. How to make sure that the file is automatically deleted after n-minutes from the file system (if the user needs to regenerate the button, he will press it).
What is the best way to implement this? Separate flow to create? Or how is it usually done? I'm using Sanic, but I think the Flask solution will work too
Answer the question
In order to leave comments, you need to log in
All files are always generated first into memory, and then saved to disk or immediately transferred from memory.
If you have a file, this is a text file generated on the fly by one thread, if the task is not overly resource-intensive. I advise instead of saving it and returning it from disk, it is better to generate and give it from memory to the user on request, skipping the stage of saving to disk.
But if you still can’t do without saving, then use any scheduling tasks, be it CRON or Celery, and store data about files in the database (path and date of creation) or run through the folder with a script and delete all files with a lifetime greater than N. But it is better to store this in the database and not torment the disk.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question