I'm currently writing a little Python3 script for monitoring parts of my macOS filesystem. It checks selected folders for new and modified files and copies them (via shutil.copy2) into a review folder which I check from time to time. The test for modification uses a comparison between the st_mtime of the original and the (already) copied files. While testing it I encountered some strange behaviour: Some files got copied over and over again, despite being unmodified.
After some poking around I found out that shutil.copy2 apparently doesn't always carry over the exact st_mtime. (I also tried out shutil.copystat explicitly, with the same result -- which isn't much of a surprise.)
To illustrate my problem: When I run the following code ...
from shutil import copy2
from os import stat
source = '/Users/me/myfile'
target = source + '-copy'
copy2(source, target)
print(stat(source).st_mtime, stat(target).st_mtime)
... the result sometimes looks like (not always):
1600616170.8300607 1600616170.83006
When I'm using the nanosecond version st_mtime_ns the result looks like:
1600616170830060720 1600616170830060000
Now my question: Does anyone know what's going on here?