Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 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 provide valuable information such as the file's age, recent changes, or when it was first introduced. In Python, you can retrieve these timestamps using functions like os.path.getctime() and os.path.getmtime(), and modify them using os.utime().
Using the os Module
The most common approach is using the os.utime() function to modify both access time and modification time of a file ?
import os
import time
# Create a sample file first
with open('sample.txt', 'w') as f:
f.write('Hello, World!')
# Get the current time
current_time = time.time()
# Set the desired access and modification datetime
access_time = current_time - 3600 # Subtract 1 hour (in seconds)
modification_time = current_time - 1800 # Subtract 30 minutes (in seconds)
# Set the access and modification datetime of the file
os.utime('sample.txt', (access_time, modification_time))
# Verify the changes
stat_info = os.stat('sample.txt')
print(f"Access time: {time.ctime(stat_info.st_atime)}")
print(f"Modification time: {time.ctime(stat_info.st_mtime)}")
The output shows the updated timestamps ?
Access time: Tue Dec 3 14:30:00 2024 Modification time: Tue Dec 3 15:00:00 2024
Using datetime Objects
For more readable code, you can use datetime objects and convert them to timestamps ?
import os
import datetime
# Create a sample file
with open('example.txt', 'w') as f:
f.write('Python file timestamp example')
# Set specific datetime values
access_datetime = datetime.datetime(2024, 1, 1, 10, 30) # January 1, 2024, 10:30 AM
modification_datetime = datetime.datetime(2024, 1, 1, 11, 0) # January 1, 2024, 11:00 AM
# Convert to timestamps
access_time = access_datetime.timestamp()
modification_time = modification_datetime.timestamp()
# Apply the new timestamps
os.utime('example.txt', (access_time, modification_time))
# Check the results
stat_info = os.stat('example.txt')
print(f"Access time: {datetime.datetime.fromtimestamp(stat_info.st_atime)}")
print(f"Modification time: {datetime.datetime.fromtimestamp(stat_info.st_mtime)}")
Access time: 2024-01-01 10:30:00 Modification time: 2024-01-01 11:00:00
Using pathlib Module
The pathlib module provides an object-oriented approach for file operations ?
import pathlib
import datetime
import os
# Create a Path object
file_path = pathlib.Path('pathlib_example.txt')
# Create the file if it doesn't exist
file_path.touch()
file_path.write_text('Created with pathlib')
# Set desired datetime values
creation_datetime = datetime.datetime(2023, 6, 15, 9, 0)
modification_datetime = datetime.datetime(2023, 6, 15, 10, 30)
# Convert to timestamps and apply
creation_time = creation_datetime.timestamp()
modification_time = modification_datetime.timestamp()
# Use os.utime with pathlib Path
os.utime(file_path, (creation_time, modification_time))
# Verify the changes
stat_info = file_path.stat()
print(f"Access time: {datetime.datetime.fromtimestamp(stat_info.st_atime)}")
print(f"Modification time: {datetime.datetime.fromtimestamp(stat_info.st_mtime)}")
Access time: 2023-06-15 09:00:00 Modification time: 2023-06-15 10:30:00
Important Notes
When working with file timestamps, keep these points in mind:
Permissions: You need appropriate file system permissions to modify timestamps
Platform differences: Creation time behavior varies across operating systems
Access vs Modification:
os.utime()sets access time and modification time, not creation time directlyPrecision: Timestamp precision depends on the file system
Comparison of Methods
| Method | Best For | Complexity |
|---|---|---|
os.utime() |
Simple timestamp operations | Low |
datetime + os.utime() |
Readable date/time specifications | Medium |
pathlib + os.utime() |
Object-oriented file handling | Medium |
Conclusion
Use os.utime() to modify file timestamps in Python. Combine it with datetime objects for readable code, or pathlib for object-oriented file operations. Remember that you're setting access and modification times, not creation time directly.
