-3

I have a file with dates, I wanted to delete all dates older than 90 days. example:

2020-02-26
2009-03-21
2021-04-12
2021-01-21

I can't think of a way to do that.

what I was able to do so far is to extract only the dates from the file and print it as a list:

file = open('dates_file', 'r')
Lines = file.readlines()

count = 0
# Strips the newline character
for line in Lines:
    count += 1
    #print(line.strip())
    index = 10
    if len(line) > index:
        line = line[0: index :]
        print(line.strip())

edit:

I have edited the script and was able to extract the date from 90 days ago to compare it to the dates in the file, and I was able to get an output of all the dates that I need to delete.

this is the script so far:

past = datetime.now().date() - timedelta(days=90)
present = datetime.now()
index = 10

file = open('dates_file', 'r')
Lines = file.readlines()


count = 0
# Strips the newline character
for line in Lines:
    count += 1
    if len(line) > index:
        line = line[0: index :]
    if line.strip() < str(past):
        print(line.strip())
Daniel
  • 17
  • 9
  • Does this answer your question? [How to compare two dates?](https://stackoverflow.com/questions/8142364/how-to-compare-two-dates) – wjandrea Dec 23 '21 at 18:50
  • once you read a line, extact the data from that string and make it to datetime object, once done you can check the difference of days there easilly – sahasrara62 Dec 23 '21 at 18:50
  • @wjandrea sadly no, i check it ad it does not seem to do what I needed it – Daniel Dec 23 '21 at 19:09
  • @Daniel In what way? It doesn't delete lines, if that's what you're referring to, but that's a separate problem. If you need help with that too, try this question: [How to delete a specific line in a file?](/q/4710067/4518341) – wjandrea Dec 23 '21 at 19:14
  • @wjandrea, it didn't help me because the date that is present to me like this: 2021-09-24 21:12:57.488002 what I need is 2021-09-24 . when I tried printing the len to match it to my format but it gave me an error: TypeError: object of type 'datetime.datetime' has no len() On how to delete files, I know how, I only need to understand of to get the dates that are older then 90 days. edit, nvm I'm an idiot I found a way to get only the date datetime.now().date() now how would you think it is better to delete the dates? – Daniel Dec 23 '21 at 19:19
  • Are you intending to modify the original file? – DarkKnight Dec 23 '21 at 19:30
  • @JCaesar yes i am – Daniel Dec 23 '21 at 19:30

1 Answers1

0

The dates in your file are ISO format giving the advantage that dates in that style can be compared lexically. Therefore you could do this:

from datetime import datetime, timedelta, date
# calculate and format a date that is 90 days prior to the current date
prev = date.strftime(datetime.today() - timedelta(days=90), '%Y-%m-%d')

with open('dates_file', 'r+') as dates:
    # build a list of dates that are greater than *prev*
    d = [s for s in dates if s.strip() > prev]
    dates.seek(0) # seek to start of file
    dates.write(''.join(d)) # write the list
    dates.truncate() # truncate
DarkKnight
  • 19,739
  • 3
  • 6
  • 22