I have two scripts, one in Python, one in Powershell, that get and compare the last modification of a file. The one in Powershell uses:
$t = $f.LastWriteTime.ToFileTimeUtc()
This time is primary for me and I need to get the same information in Python. I am using os.stat
and convert the UNIX timestamp to 'Windows File Time'* using this formula:
statinfo = os.stat(file_name)
t = long(statinfo.st_mtime * 10000000L) + 11644473600L * 10000000L
However, I run into problems with rounding errors. st_mtime
is a float and when I multiply it and cast to long, I am losing precision -- typically the error is less than 10 (i.e. less then 1 millisecond). I can of course fix my program so that it compares the numbers within this precision, but I would much rather have the same numbers.
There is a similar question on SO here: How do I get *change* file time in Windows? from which I gather I would have to access the structure FILE_BASIC_INFORMATION (http://msdn.microsoft.com/en-us/library/windows/hardware/ff545762(v=vs.85).aspx), however I am not sure how to do it without using IronPython, PyWin or similar Python extensions. Is there an easy way (maybe using ctypes
) to access this information?
*A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC). See https://msdn.microsoft.com/en-us/library/system.datetime.tofiletimeutc(v=vs.110).aspx