i have 2 scripts, 1 in python, 1 in powershell, , compare last modification of file. 1 in powershell uses:
$t = $f.lastwritetime.tofiletimeutc()
this time primary me , need same information in python. using os.stat
, convert unix timestamp 'windows file time'* using formula:
statinfo = os.stat(file_name) t = long(statinfo.st_mtime * 10000000l) + 11644473600l * 10000000l
however, run problems rounding errors. st_mtime
float , when multiply , cast long, losing precision -- typically error less 10 (i.e. less 1 millisecond). can of course fix program compares numbers within precision, rather have same numbers.
there similar question on here: how *change* file time in windows? gather have access structure file_basic_information (http://msdn.microsoft.com/en-us/library/windows/hardware/ff545762(v=vs.85).aspx), not sure how without using ironpython, pywin or similar python extensions. there easy way (maybe using ctypes
) access information?
*a windows file time 64-bit value represents number of 100-nanosecond intervals 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
so, benefit of problem. actually, turns out question mentioned, how *change* file time in windows?, bit misleading, mentions obscure driver access function zwqueryinformationfile
, while there plain getfiletime
function can use. either way, these functions require file handle , in turn requires calling createfile
, closehandle
, large number of files (about 1 million in case) quite expensive. in end used getfileattributesexw
requires path. measurements still little bit slower (about 10%) os.stat
, think down overhead of calling functions through ctypes
.
so after copy-pasting internet, put following code wanted.
from ctypes import windll, structure, byref ctypes.wintypes import lpwstr, dword, filetime class win32_file_attribute_data(structure): _fields_ = [("dwfileattributes", dword), ("ftcreationtime", filetime), ("ftlastaccesstime", filetime), ("ftlastwritetime", filetime), ("nfilesizehigh", dword), ("nfilesizelow", dword)] filename = 'path , file name' wfad = win32_file_attribute_data() getfileexinfostandard = 0 windll.kernel32.getfileattributesexw(lpwstr(filename), getfileexinfostandard, byref(wfad)) lowtime = long(wfad.ftlastwritetime.dwlowdatetime) hightime = long(wfad.ftlastwritetime.dwhighdatetime) filetime = (hightime << 32) + lowtime print filetime
Comments
Post a Comment