代码语言:javascript复制
from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandle
from win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING
from pywintypes import Time
import time
import os
# -*- coding: utf-8 -*-
def modifyFileTime(filePath,createTime,modifyTime,accessTime,offset):
try:
format = "%Y-%m-%d %H:%M:%S" #时间格式
cTime_t = timeOffsetAndStruct(createTime,format,offset[0])
mTime_t = timeOffsetAndStruct(modifyTime,format,offset[1])
aTime_t = timeOffsetAndStruct(accessTime,format,offset[2])
fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)
createTimes, accessTimes, modifyTimes = GetFileTime(fh)
createTimes = Time(time.mktime(cTime_t))
accessTimes = Time(time.mktime(aTime_t))
modifyTimes = Time(time.mktime(mTime_t))
SetFileTime(fh, createTimes, accessTimes, modifyTimes)
CloseHandle(fh)
return 0
except:
return 1
def timeOffsetAndStruct(times,format,offset):
return time.localtime(time.mktime(time.strptime(times, format)) offset)
#调用
cTime = "2020-12-09 18:32:02" # 创建时间
mTime = "2020-12-09 18:32:02" # 修改时间
aTime = "2020-12-09 18:32:02" # 访问时间
offset = (0,1,2) # 偏移的秒数
#r=modifyFileTime(fName,cTime,mTime,aTime,offset)
root = '' #填入要修改的所有文件的主目录 绝对路径
path = os.path.join(root)
pathnames = []
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in filenames:
pathnames = [os.path.join(dirpath, filename)]
for file in pathnames:
print(file)
check= modifyFileTime(file,cTime,mTime,aTime,offset)
print("%s ok"%(file) if check==0 else "%s not ok"%(file))