How to set creation and modification date/time of a file using Python?


The creation and modification datetime of a file in Python are defined as the timestamps associated with the events of when the file was created and when it was last modified.

Creation datetime: It is defined as the timestamp when a file was initially created or added to the file system.

Modification datetime: It is defined as the timestamp when the file's content was last modified or updated.

These datetimes can provide a lot of information such as the file's age, recent changes, or when it was first introduced.

In Python, you can fetch the creation and modification datetimes of a file by using functions like os.path.getctime() and os.path.getmtime().

By closely examining these timestamps, you can track file changes, manage versions, or perform actions based on how recently these modifications were carried out.

To set the creation and modification datetimes of a file using Python, you utilize the os module and its os.utime() function. The os.utime() function makes it possible for you to modify the access time and modification time of a file.

Making Use of the os Module

Example

Step-wise explanations of the code:

  • The necessary modules like os module are imported for working with the operating system and the time module is also imported for working with time-related functions.

  • The path of the file for which you want to set the creation and modification datetime is specified.

  • The current time is accessed using time.time(). This function returns the current time in seconds since the epoch (January 1, 1970).

  • The desired creation and modification datetimes are set; for this, a certain amount of time from the current time is subtracted. In the example above, 1 hour from the creation time and 30 minutes from the modification time are subtracted.

  • The os.utime() function of the os module is made use of to set the creation and modification datetime of the file. The file path is passed as the first argument and a tuple containing the creation time and modification time is passed as the second argument.

  • When you execute this code, you will be able to set the creation and modification datetimes of the specified file to the desired values.

  • It is to be noted that setting the creation and modification datetime of a file might require appropriate permissions on the file system. You make sure you have the necessary permissions to modify the datetime information of the file.

import os
import time

# Specify the file path
file_path = 'path/to/file.txt'

# Get the current time
current_time = time.time()

# Set the desired creation and modification datetime
creation_time = current_time - 3600  # Subtract 1 hour 
(in seconds)
modification_time = current_time - 1800  # Subtract 30 
minutes (in seconds)

# Set the creation and modification datetime of the file
os.utime(file_path, (creation_time, modification_time))

Making Use of the shutil Module

Yet another approach to set the creation and modification datetime of a file is by utilizing the shutil module; it has high-level file operations to help us in our task. Here's an example:

Example

The steps to be taken are as follows

  • Required modules to be imported: shutil module for high-level file operations and datetime module for working with date and time objects.

  • The path of the file you want to modify is specified.

  • datetime objects for the desired creation and modification datetime are created. In this example, we set the creation time to January 1, 2022, 10:30 AM, and the modification time to January 1, 2022, 11:00 AM.

  • The datetime objects are converted to timestamps using the timestamp() method.

  • The shutil.cmp() function is used to set the creation and modification datetime of the file. The file path is passed as the first argument and a tuple containing the creation time and modification time is passed as the second argument.

  • By running this code, you can set the creation and modification datetime of the specified file to the desired values.

import shutil
import datetime

# Specify the file path
file_path = 'path/to/file.txt'

# Get the desired creation and modification datetime
creation_datetime = datetime.datetime(2022, 1, 1, 10, 30)  
# January 1, 2022, 10:30 AM
modification_datetime = datetime.datetime(2022, 1, 1, 11, 0)  
# January 1, 2022, 11:00 AM

# Convert the datetime objects to timestamps
creation_time = creation_datetime.timestamp()
modification_time = modification_datetime.timestamp()

# Set the creation and modification datetime of the file
shutil.cmp(file_path, (creation_time, modification_time))

Making Use of the pathlib Module

The pathlib module is imported. In this example, we utilize pathlib to set the creation and modification datetime of a file:

Example

  • Import the necessary modules: pathlib module for working with file paths and datetime module for manipulating dates and times.

  • Specify the path of the file you want to modify using pathlib.Path.

  • Create datetime objects for the desired creation and modification datetime.

  • Use the touch() method of the Path object to create the file if it doesn't exist.

  • Access the st_ctime (creation time) and st_mtime (modification time) attributes of the file's stat object and set them to the timestamps of the desired datetime values.

  • By executing this code, you will be able to set the creation and modification datetime of the specified file using pathlib.

import pathlib
import datetime

# Specify the file path
file_path = pathlib.Path('path/to/file.txt')

# Get the desired creation and modification datetime
creation_datetime = datetime.datetime(2022, 1, 1, 10, 30)  
# January 1, 2022, 10:30 AM
modification_datetime = datetime.datetime(2022, 1, 1, 11, 0)  
# January 1, 2022, 11:00 AM

# Set the creation and modification datetime of the file
file_path.touch()
file_path.stat().st_ctime = creation_datetime.timestamp()
file_path.stat().st_mtime = modification_datetime.timestamp()

In short, we've explored multiple approaches to set the creation and modification datetime of a file using Python. Whether you choose to use the os module, shutil, pathlib, you now have the knowledge to manipulate the datetime information of files in your Python programs. You can always choose the approach that is most appropriate for your needs and start managing file datetimes with ease.

Updated on: 25-Jul-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements