Fundamentals 7 min read

How to Change File Creation, Modification, and Access Times on Windows with Python

This guide explains how to view and modify a file's creation, modification, and access timestamps on Windows using Python's os module, os.utime, and the pywin32 library, including code examples and practical considerations.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Change File Creation, Modification, and Access Times on Windows with Python

Modifying File Time Attributes (S Series)

S, also called "Small", is a series of short technical notes documenting useful tricks discovered during daily work.

Viewing File Time Attributes

On Windows, right‑click a file and select Properties to see the creation, modification, and access times.

The three timestamps behave as their names suggest: the access time changes on any file operation, the modification time changes when the file is saved, and the creation time is set when the file is first created and is difficult to alter directly.

Modifying File Time Attributes with Python

Use os.stat to retrieve a file's basic attributes, which include the three timestamps.

import os
os.stat(r"C:\Users\admin\Desktop\1231.xlsx")

These timestamps are stored as Unix timestamps; they are read‑only and cannot be changed directly.

The os.utime function can modify the access and modification times, accepting timestamps as arguments.

from datetime import datetime

a_time = datetime(2024, 4, 9).timestamp()
m_time = datetime(2024, 5, 1).timestamp()
# Set access time to 2024‑04‑09 and modification time to 2024‑05‑01
os.utime(r"C:\Users\admin\Desktop\1231.xlsx", (a_time, m_time))

After running os.utime, the file properties show the updated modification time. The access time is also updated, but because the file is accessed during the operation, it reflects the current time.

To modify the creation time, the standard os module provides no direct function. Instead, use the win32file module from the pywin32 package.

from win32file import CreateFile, SetFileTime, CloseHandle, GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING
from datetime import datetime

def modifyFileTime(filepath, ctime, mtime, atime, format_str="%Y%m%d %H:%M:%S"):
    """Modify a file's creation, modification, and access timestamps.
    filepath: str – file path
    ctime, mtime, atime: str – timestamps in the given format
    format_str: str – default "%Y%m%d %H:%M:%S"
    """
    fh = CreateFile(filepath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)
    createTimes = datetime.strptime(ctime, format_str)
    accessTimes = datetime.strptime(atime, format_str)
    modifyTimes = datetime.strptime(mtime, format_str)
    SetFileTime(fh, createTimes, accessTimes, modifyTimes)
    CloseHandle(fh)

Example usage:

modifyFileTime(r"C:\Users\admin\Desktop\1231.xlsx", '20240501 01:01:01', '20240501 02:01:01', '20240501 03:01:01')

This sets the creation time to 2024‑05‑01 01:01:01, the modification time to 2024‑05‑01 02:01:01, and the access time to 2024‑05‑01 03:01:01.

All three timestamps are successfully updated.

Alternatively, you can combine os.utime for access/modification times with win32_setctime.setctime for creation time, and use dateutil.parser.parse to handle string timestamps.

import os
from win32_setctime import setctime
from dateutil.parser import parse

def modifytime(filename, ctime, mtime, atime):
    ctime = parse(ctime).timestamp()
    atime = parse(atime).timestamp()
    mtime = parse(mtime).timestamp()
    setctime(filename, ctime)
    os.utime(filename, (atime, mtime))

Example:

modifytime(r"C:\Users\admin\Desktop\1231.xlsx", '20240502 10:00:00', '20240502 11:00:00', '20240502 12:00:00')

Conclusion

By experimenting with these small scripts, you can modify a file's timestamps on Windows. The notes are kept for reference only; altering timestamps arbitrarily is not recommended.

2022.5.30

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonWindowsOS modulefile timestampsfile metadatapywin32
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.