import time
from pywintypes import Time # 可以忽视这个 Time 报错(运行程序还是没问题的)
from win32con import FILE_FLAG_BACKUP_SEMANTICS
from win32con import FILE_SHARE_WRITE
from win32file import CloseHandle
from win32file import CreateFile
from win32file import GENERIC_WRITE
from win32file import OPEN_EXISTING
from win32file import SetFileTime
def modify_file_create_time(filename, create_time_str, update_time_str, access_time_str):
try:
format_str = "%Y-%m-%d %H:%M:%S" # 时间格式
# f = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)
f = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, 0)
create_time = Time(time.mktime(time.strptime(create_time_str, format_str)))
update_time = Time(time.mktime(time.strptime(update_time_str, format_str)))
access_time = Time(time.mktime(time.strptime(access_time_str, format_str)))
SetFileTime(f, create_time, update_time, access_time)
CloseHandle(f)
#print('update file time success:{}/{}/{}'.format(create_time_str, update_time_str,
access_time_str))
except Exception as e:
print('update file time fail:{}'.format(e))
if __name__ == '__main__':
fName = ''
timeNew = 1651438000
time_array = time.localtime(timeNew)
time_str = time.strftime("%Y-%m-%d %H:%M:%S",time_array)
cTime = time_str # 创建时间
mTime = time_str # 修改时间
aTime = time_str # 访问时间
modify_file_create_time(fName, cTime, mTime, aTime)
python修改文件创建时间、修改时间、访问时间
最新推荐文章于 2024-08-23 18:41:29 发布