Home > other >  How to I check if last modification date of a file is equal or bigger than five minutes ago?
How to I check if last modification date of a file is equal or bigger than five minutes ago?

Time:08-09

I need to check if a file was modified in the last five minutes and then execute some code.

What I have right now is this code example I'm working on. It's getting there but I can't execute it because

  File "c:\Users\CatarinaRibeiro\Desktop\from datetime import date.py", line 8, in <module>
    modificationTime = datetime.datetime(time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(os.path.getmtime(configFilePath))))
TypeError: 'str' object cannot be interpreted as an integer

I know it's because of the data types. I can't figure out other ways to make this work. Do you have any ideas?


from datetime import date
import datetime
import os
import time

configFilePath = "C:\\Users\\CatarinaRibeiro\\Pictures\\2.png"

modificationTime = datetime.datetime(time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(os.path.getmtime(configFilePath))))
print("Last modification date") 
print(modificationTime)


today = datetime.datetime.now() 
print("Today")
print(today)
TimeRange = (today) - (datetime.timedelta(minutes=5))
print(TimeRange)
Validation = (modificationTime >= str(TimeRange))


if not Validation:
    print("No new updates on the configuration file")
    print(Validation)

else:
    print("UPDATED")
    print(Validation)

CodePudding user response:

Change line 8 with

modificationTime = datetime.datetime.fromtimestamp(int(os.path.getmtime(configFilePath)))
  • Related