简体   繁体   中英

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

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)

Change line 8 with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM