If I run the following code
import osimport timedef check_different_times(): try: os.remove("temp") except FileNotFoundError: pass with open("temp", "w") as f: f.write("hi") first = os.stat("temp").st_mtime_ns time.sleep(0.001) with open("temp", "w") as f: f.write("b") second = os.stat("temp").st_mtime_ns return first != secondprint(sum(check_different_times() for _ in range(100)))
Based on my understanding of how unix timestamps work, this should print 100, given that 0.001s is 1e6 nanoseconds, so the two modification times should obviously be different.
However, if I run this on my Ubuntu laptop, it prints something like 28 or 30. If I remove the sleep
line it prints something like 1 or 2. Is this some weird buffering thing, a bug in Ubuntu, in Python, or a flaw in my understanding of what mtime is?