0

I am trying to do something with directories older than 4 days. Here is what I have:

from datetime import datetime, date


#get current time
curret_time = datetime.now() 

#get file creation time
stat = os.stat(my_directory) 
creation_time = datetime.fromtimestamp(stat.st_birthtime)

#get the age of directory
age_of_directory=curret_time - creation_time

#I want to remove any directory that is older than 4 days
if age_of_directory > 4:
    #shutil.rmtree(my_directory) 
    print(age_of_directory) #debugging line

Error I get is:

TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'

How do a fix this issue?

Supertech
  • 746
  • 1
  • 9
  • 25
  • 1
    _Don't_ compare a timedelta (`age_of_directory`) with an integer (`4` - but four _what_, exactly)? – jonrsharpe Jan 02 '23 at 22:52
  • Too bad this question got closed as a duplicate. In answering this question, I was led to a HUGE SECRET about how to convert Python `timedelta` objects to arbitrary numeric time values. As this wasn't the question asked here, it isn't technically wrong to close this question as a duplicate. But doing so hides some amazing information about the RIGHT WAY to do something in Python that I bet most Python programmers don't know about. I wish I knew where copy this information that i learned about only today such that many other Python programmers would come across and learn about this technique – CryptoFool Jan 03 '23 at 00:20
  • Its marked as similar to the question here:"https://stackoverflow.com/questions/2591845/comparing-a-time-delta-in-python". When was asked, I could say not similar. But looks like too late. Cannot you post there as anser? – Supertech Jan 03 '23 at 00:29
  • 1
    @Supertech - I have realized that what I thought was SO AMAZING! here isn't as amazing as I thought it was. I got caught up thinking there was some fancy time-unit logic going on in my example. There wasn't. Dividing one `timedelta` from another is still a cool thing to know about, but it's not as complicated and cool an idea as I first thought. I may look for a place in SO where this technique could improve upon a complicated answer...maybe the marked dup...maybe something else. – CryptoFool Jan 03 '23 at 00:50

1 Answers1

4

Just convert 4 to a timedelta:

from datetime import timedelta
...
if age_of_directory > timedelta(days=4):

UPDATE

Of course, one could go the other way and convert age_of_directory to a fractional number of days and then compare that to 4.

You can convert a timedelta object to a float by dividing one timedelta by another. Here would be how to do so in this case:

age_of_directory_in_days = age_of_directory / timedelta(days=1)
if age_of_directory_in_days > 4:

NOTE: You might have seen a bulkier answer here if you saw an earlier version of my answer. I found something I thought was really special about dividing one timedelta object by another. But later, I thought about it some more and realized that it wasn't as earth shatteringly cool as I'd first thought.

The take-away here, which isn't all that surprising, is that dividing timedelta objects does pretty much exactly what division is supposed to do. The result is the number of times that the denominator goes into the numerator. Nothing all that special here. I got all confused in thinking about "time units". But that doesn't really enter into it. Dividing two time periods results in something that is dimensionless, as does dividing any two quantities that are in the same unit of measure.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44