0

Is it possible to create a file in Python with a given expiration date (let's say around one week)?

I gave a look at tempfile, but - if I'm not wrong - the files created by thet are destroyed as soon as they get closed.

As a bonus question, if the only way was to do this manually, which is the best practice to store package-related files? Where should I store them?

Luca
  • 1,610
  • 1
  • 19
  • 30
  • Do you want these tempfiles to be stored on disk or RAM? – Learning is a mess Jan 30 '19 at 11:13
  • On disk. I edited the question to make it clearer. They should last about one week – Luca Jan 30 '19 at 11:14
  • Which OS are you using? – Farhan.K Jan 30 '19 at 11:16
  • Windows 10. But would have preferred a OS agnostic solution – Luca Jan 30 '19 at 11:16
  • Would suggest to create custom properties for each file putting an encrypted expiry date on the same. Then create a background process to decrypt the date and delete if it is past its expiration. Only issue is that it should work at specific folder or position( as you may not want a background process to look for all files on the hard drive :)) – BlindSniper Jan 30 '19 at 11:19
  • You can get the creation time of a file using this answer: https://stackoverflow.com/a/39501288/5270506. The add 7 days to that time and compare it with the current time and delete the file if it's older than that. The reason I asked about OS is because it's not very easy to get the creation date of a file in Linux – Farhan.K Jan 30 '19 at 11:21
  • My current solution was to store them in a temporary folder and delete them every time the function runs – Luca Jan 30 '19 at 11:22
  • 1
    You will have to run the script as a service which periodically checks the files. – Farhan.K Jan 30 '19 at 11:23
  • 1
    Ok. I don't want to do that, but - nonetheless - that's the answer to my question – Luca Jan 30 '19 at 11:25
  • @LucaAmerio you may mark an answer to close the question if you've found the answer. cheers – DirtyBit Jan 30 '19 at 11:48

1 Answers1

2

How about setting the time to delete a file (assuming you've created it already):

import os
import datetime
from datetime import date

dateTimeStart = "2019-02-01 12:00:00"
dateTime1 = datetime.strptime(dateTimeStart, "%Y-%m-%d %H:%M:%S")

if dateTime1.date() > date.today():
    os.remove("/tmp/<created_file>.txt")
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • This is very similar to my current solution, but only clean the file when the script is executed. If the script is not executed for 6 month, the files will stay there – Luca Jan 30 '19 at 11:21
  • @LucaAmerio So does it not help? You could schedule the script in the Task Scheduler to run preodically – DirtyBit Jan 30 '19 at 11:22
  • Well, it's a solution, but I was ideally hoping to find a way to delete the file after the expiration date independently from the fact that the script is run again or not. If this is not possible, I'm afraid this is the best possible solution – Luca Jan 30 '19 at 11:24
  • 1
    @LucaAmerio I'm afraid it isn't possible if the script isn't running, you can schedule the script in the Task Scheduler for it to run periodically, that way you don't have to worry about the six months gap. – DirtyBit Jan 30 '19 at 11:25