Last Modified Time function returns 0.0 for existing valid files – ‘getmtime’ + ‘stat.st__mtime’
I have a cache engine to check when files were last modified.
The engine has been working well, but recently my tests started failing with a specific file.
The getmtime
() function (or stat.st_mtime) returns zero, even if the file exists and has a last modified time. All other failures in the folder return the expected float epoch time value corresponding to the last mod datetime.
Any ideas on what could be the cause?
The documentation doesn’t mention anything about a zero return value, only that it returns a float. What causes Python to return 0.0?
Given an existing file path
(see attributes below):
>>> os.path.exists(filepath)
True
>>> os.path.getmtime(cache_filepath)
0.0
>>> os.stat(filepath)
os.stat_result(st_mode=33206, st_ino=1125899907202573, st_dev=2898260115, st_nlink=1, st_uid=0, st_gid=0, st_size=36, st_atime=0, st_mtime=0, st_ctime=1508902786)
>>> os.stat(filepath).st_time
0.0
Solution
My error – the file in question is actually set to 0.0 mtime:
PS: Side note – the reason I’m getting into this is because datetime.fromtimestamp
fails when it gets a value of 0.0, which turns out to be a Python 3 error:
https://bugs.python.org/issue29097