B
B
beduin012019-01-23 17:52:43
Python
beduin01, 2019-01-23 17:52:43

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

3 answer(s)
A
Alexander, 2019-01-23
@beduin01

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.

V
Vadim Shatalov, 2019-01-23
@netpastor

Celery
Schedule
Cron after all

V
Vladimir, 2019-01-23
@vintello

you have the date the file was created. it is from it that
the Kronov task should be based, which scans the desired folder and demolishes files older than 5 minutes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question