如何从Python更改Windows文件的文件创建日期?

Cla*_*diu 29 python windows time date file

如何从Python更改Windows文件的文件创建日期?

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)

  • 如果你得到一个ImportError并想知道你在哪里可以找到`pywintypes`(正如我所做的那样):http://sourceforge.net/projects/pywin32/ (4认同)

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)


per*_*507 5

首先安装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)

  • 为什么很难发现 `win32file` 是 `pywin32` 的一部分?谷歌让我心烦意乱,这意味着其他答案都毫无用处;他们假设你已经安装了它。感谢您在答案顶部的有用提示。 (2认同)

Del*_*gan 5

我不想只带整个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