Comment modifier la date de création d'un fichier Windows à partir de Python ?
Réponses
Trop de publicités?Rasage de yack pour la victoire.
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()
percy507
Points
360
installer l'extension 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)
Delta
Points
794
import os
os.utime(path, (accessed_time, modified_time))
http://docs.python.org/library/os.html
Au moins, il modifie le temps de modification, sans utiliser le module win32.