I wrote this code in c# to check if a file is out of date:
DateTime? lastTimeModified = file.getLastTimeModified();
if (!lastTimeModified.HasValue)
{
//File does not exist, so it is out of date
return true;
}
if (lastTimeModified.Value < DateTime.Now.AddMinutes(-synchIntervall))
{
return true;
} else
{
return false;
}
How do I write this in python?
I tried this in python.
statbuf = os.stat(filename)
if(statbuf.st_mtime < datetime.datetime.now() - self.synchIntervall):
return True
else:
return False
I got the following exception
message str: unsupported operand type(s) for -: 'datetime.datetime' and 'int'
Use stat. M_TIME to get the last modified time and subtract it from the current time.
To measure time elapsed during program's execution, either use time. clock() or time. time() functions. The python docs state that this function should be used for benchmarking purposes.
The os.path.getmtime ('file_path') function returns a modification time in numeric timestamp in float. Pass ‘file path’ as an absolute or relative path to a file. Wrap creation and modification time in a datetime object. The modification time returned by the getmtime () is in a numeric timestamps format.
To detect a file older than 3 months we can either approximate to 90 days: from datetime import timedelta is_file_older_than (filename, timedelta (days=90)) from dateutil.relativedelta import relativedelta # pip install python-dateutil is_file_older_than (filename, relativedelta (months=3))
pathlib.Path ('file_path').stat ().st_ctime: To get file creation time but only on windows and recent metadata change time on Unix The below steps show how to use the os.path module and datetime module to get the creation and modification time of a file in Python. The os.path module implements some valuable functions on pathnames.
It can be obtained in Unix time (Epoch time, Posix time) but can be converted to date and time using the datetime module. os.stat_result — Miscellaneous operating system interfaces — Python 3.10.0 documentation You can get the following timestamps. The meaning differs depending on the OS, so be especially careful about the creation time.
Here is a generic solution using timedelta
from datetime import datetime
def is_file_older_than (file, delta):
cutoff = datetime.utcnow() - delta
mtime = datetime.utcfromtimestamp(os.path.getmtime(file))
if mtime < cutoff:
return True
return False
This can be used as follows.
To detect a file older than 10 seconds:
from datetime import timedelta
is_file_older_than(filename, timedelta(seconds=10))
To detect a file older than 10 days:
from datetime import timedelta
is_file_older_than(filename, timedelta(days=10))
If you are ok installing external dependencies, you can also do months and years:
from dateutil.relativedelta import relativedelta # pip install python-dateutil
is_file_older_than(filename, relativedelta(months=10))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With