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())