How to Get the Creation and Modification Date of a File Using Python
In Python, you can get the creation and modification date of a file using the os.path
module and the os.stat()
method. The getctime()
function retrieves the file creation time, while the getmtime()
function returns the last modification time. These timestamps are typically represented in seconds since the epoch, but you can format them into a readable date format using the datetime
module.
Examples to Get the Creation and Modification Date of a File
1. Get the Creation and Modification Date Using os.path.getctime()
and os.path.getmtime()
In this example, we will use os.path.getctime()
to get the file creation time and os.path.getmtime()
to get the file modification time. We will then format these timestamps into a human-readable format using the datetime.datetime.fromtimestamp()
method.
The following screenshot contains some of the details of example.txt
.

main.py
import os
import datetime
# Specify the file path
file_path = "example.txt"
# Get the creation and modification time
creation_time = os.path.getctime(file_path)
modification_time = os.path.getmtime(file_path)
# Convert timestamps to human-readable format
creation_date = datetime.datetime.fromtimestamp(creation_time)
modification_date = datetime.datetime.fromtimestamp(modification_time)
# Print the results
print("File Creation Date:", creation_date)
print("File Modification Date:", modification_date)
Explanation:
- We import the
os
module to access file system properties anddatetime
to format the output. - The variable
file_path
stores the path of the file whose timestamps we want to retrieve. - The
os.path.getctime(file_path)
function gets the creation time in seconds since the epoch. - The
os.path.getmtime(file_path)
function gets the modification time in seconds since the epoch. - The
datetime.datetime.fromtimestamp()
function converts these timestamps into human-readable date and time format.
Output:

2. Get File Timestamps Using os.stat()
In this example, we use the os.stat()
method, which provides detailed file metadata, including the creation and modification timestamps.
main.py
import os
import datetime
# Specify the file path
file_path = "example.txt"
# Get file stats
file_stats = os.stat(file_path)
# Extract creation and modification timestamps
creation_time = file_stats.st_ctime
modification_time = file_stats.st_mtime
# Convert timestamps to human-readable format
creation_date = datetime.datetime.fromtimestamp(creation_time)
modification_date = datetime.datetime.fromtimestamp(modification_time)
# Print the results
print("File Creation Date:", creation_date)
print("File Modification Date:", modification_date)
Explanation:
- We import the necessary modules:
os
for file metadata anddatetime
for formatting timestamps. - The variable
file_path
stores the file’s location. - The
os.stat(file_path)
function retrieves file metadata and stores it infile_stats
. - The
file_stats.st_ctime
gives the file’s creation time. - The
file_stats.st_mtime
provides the last modification time. - Both timestamps are converted into human-readable format using
datetime.datetime.fromtimestamp()
.
Output:

3. Formatting File Timestamps in a Custom Format
In this example, we customize the date format of the file timestamps using strftime()
for better readability.
main.py
import os
import datetime
# Specify the file path
file_path = "example.txt"
# Get creation and modification time
creation_time = os.path.getctime(file_path)
modification_time = os.path.getmtime(file_path)
# Convert timestamps to custom format
custom_format = "%Y-%m-%d %H:%M:%S"
creation_date = datetime.datetime.fromtimestamp(creation_time).strftime(custom_format)
modification_date = datetime.datetime.fromtimestamp(modification_time).strftime(custom_format)
# Print the results
print("File Creation Date:", creation_date)
print("File Modification Date:", modification_date)
Explanation:
- We define a custom date-time format using the
strftime()
function. - The format
"%Y-%m-%d %H:%M:%S"
displays timestamps in a more readable format (e.g.,2025-02-10 14:35:22
). - The
strftime()
method is applied to both timestamps to format them accordingly.
Output:

Conclusion
In summary, Python provides multiple ways to retrieve file creation and modification timestamps:
os.path.getctime()
andos.path.getmtime()
: Simple methods to get creation and modification times.os.stat()
: A comprehensive way to access file metadata.- Custom Formatting: Using
strftime()
for better readability.