修改文件时间
2019-09-04 python 1309
from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandle from win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING import time import os from pywintypes import Time def modifyFileTime(filePath, createTime=None, modifyTime=None, accessTime=None, offset=(0,0,0)): """ 用来修改任意文件的相关时间属性,时间格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02 :param filePath: 文件路径名 :param createTime: 创建时间 :param modifyTime: 修改时间 :param accessTime: 访问时间 :param offset: 时间偏移的秒数,tuple格式,顺序和参数时间对应 """ try: format = "%Y-%m-%d %H:%M:%S" # 时间格式 fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTimes, accessTimes, modifyTimes = GetFileTime(fh) if createTime is not None: cTime_t = timeOffsetAndStruct(createTime, format, offset[0]) createTimes = Time(time.mktime(cTime_t)) if accessTime is not None: aTime_t = timeOffsetAndStruct(accessTime, format, offset[2]) accessTimes = Time(time.mktime(aTime_t)) if modifyTime is not None: mTime_t = timeOffsetAndStruct(modifyTime, format, offset[1]) modifyTimes = Time(time.mktime(mTime_t)) SetFileTime(fh, createTimes, accessTimes, modifyTimes) CloseHandle(fh) return True except Exception as e: print(str(e)) return False def timeOffsetAndStruct(times, format, offset): return time.localtime(time.mktime(time.strptime(times, format)) + offset)
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论