Cla*_*diu 32
牦牛剃须获胜.
import pywintypes, win32file, win32con
def changeFileCreationTime(fname, newtime):
wintime = pywintypes.Time(newtime)
winfile = win32file.CreateFile(
fname, win32con.GENERIC_WRITE,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None, win32con.OPEN_EXISTING,
win32con.FILE_ATTRIBUTE_NORMAL, None)
win32file.SetFileTime(winfile, wintime, None, None)
winfile.close()
Run Code Online (Sandbox Code Playgroud)
kub*_*505 11
filedate模块可能会满足您的需求。优点:
pip install filedate
Run Code Online (Sandbox Code Playgroud)
import filedate
Path = "~/Documents/File.txt"
filedate.File(Path).set(
created = "1st February 2003, 12:30",
modified = "3:00 PM, 04 May 2009",
accessed = "08/07/2014 18:30:45"
)
Run Code Online (Sandbox Code Playgroud)
首先安装pywin32扩展 https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/
import win32file
import pywintypes
# main logic function
def changeFileCreateTime(path, ctime):
# path: your file path
# ctime: Unix timestamp
# open file and get the handle of file
# API: http://timgolden.me.uk/pywin32-docs/win32file__CreateFile_meth.html
handle = win32file.CreateFile(
path, # file path
win32file.GENERIC_WRITE, # must opened with GENERIC_WRITE access
0,
None,
win32file.OPEN_EXISTING,
0,
0
)
# create a PyTime object
# API: http://timgolden.me.uk/pywin32-docs/pywintypes__Time_meth.html
PyTime = pywintypes.Time(ctime)
# reset the create time of file
# API: http://timgolden.me.uk/pywin32-docs/win32file__SetFileTime_meth.html
win32file.SetFileTime(
handle,
PyTime
)
# example
changeFileCreateTime('C:/Users/percy/Desktop/1.txt',1234567789)
Run Code Online (Sandbox Code Playgroud)
我不想只带整个pywin32/ win32file库来设置文件的创建时间,所以我制作了win32-setctime可以做到这一点的软件包。
pip install win32-setctime
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
from win32_setctime import setctime
setctime("my_file.txt", 1561675987.509)
Run Code Online (Sandbox Code Playgroud)
基本上,该函数可以减少到仅几行,而不需要除内置ctypesPython库以外的任何依赖项:
from ctypes import windll, wintypes, byref
filepath = "my_file.txt"
epoch = 1561675987.509
timestamp = int((epoch * 10000000) + 116444736000000000)
ctime = wintypes.FILETIME(timestamp & 0xFFFFFFFF, timestamp >> 32)
handle = windll.kernel32.CreateFileW(filepath, 256, 0, None, 3, 128, None)
windll.kernel32.SetFileTime(handle, byref(ctime), None, None)
windll.kernel32.CloseHandle(handle)
Run Code Online (Sandbox Code Playgroud)
有关高级管理(例如错误处理),请参见的源代码win32_setctime.py。