There are multiple ways to get file creation and modification datetime in Python. We will use the following methods of an OS and pathlib module to get file modification and creation time in Python.
os.path module:
os.path.getmtime(path)
: Cross-platform way to get file modification time in Python. It returns the Unix timestamp of when the file was last modified.os.path.getctime('file_path')
: To get file creation time but only on windows.os.stat(path).st_birthtime
: To get file creation time on Mac and some Unix based systems.
Pathlib module:
pathlib.Path('file_path').stat().st_mtime
: Best cross-platform way to get file modification time in Python.pathlib.Path('file_path').stat().st_ctime
: To get file creation time but only on windows and recent metadata change time on Unix
Table of contents
How to Get File Modification and Creation Time in Python
The below steps show how to use the os.path module and datetime module to get the creation and modification time of a file in Python.
- Import os.path module and datetime module
The
os.path
module implements some valuable functions on pathnames. It is helpful to get the metadata of a file.
DateTime module used to convert the creation and modification time returned by theos.path
module to human-readable format such as Day, year, months, hours, minutes, and seconds (dd-mm-YYYY HH:MM:SS). - Use getmtime() function to get a modification time
The
os.path.getmtime('file_path')
function returns a modification time in numeric timestamp in float. Pass ‘file path’ as an absolute or relative path to a file. - Wrap creation and modification time in a datetime object.
The modification time returned by the
getmtime()
is in a numeric timestamps format. Use thefromtimestamp()
method of a datetime class to format it in a human-readable format (dd-mm-YYYY HH:MM:SS). - Use getctime() function to get a creation time
On windows, the
os.path.getmtime('file_path')
function returns the creation time of a file.
On the other hand, on Unix, it will not work. Instead, it will return the timestamp of the last time when the file’s attributes or content were changed (most recent metadata change on Unix).
Example to Get File Modification and Creation Time
import datetime
import os
# Path to the file
path = r"E:\demos\files_demos\sample.txt"
# file modification timestamp of a file
m_time = os.path.getmtime(path)
# convert timestamp into DateTime object
dt_m = datetime.datetime.fromtimestamp(m_time)
print('Modified on:', dt_m)
# file creation timestamp in float
c_time = os.path.getctime(path)
# convert creation timestamp into DateTime object
dt_c = datetime.datetime.fromtimestamp(c_time)
print('Created on:', dt_c)
Output:
Modified on: 2021-07-02 16:47:50.791990 Created on: 2021-06-30 17:21:57.914774
Note: If you want to represent datetime in different formats see Python Datetime formatting.
Get File Creation Datetime on Mac and Unix Systems
- On Mac, as well as some Unix based systems, you can use the
st_birthtime
attribute of aos.stat()
or ( fsta()/lstat()) to function get the file creation time. - But, the
st_birthtime
attribute of aos.stat()
isnt’ guaranteed to be available on all systems such as linux. - We need to convert the the integer tmestamp returned by the
st_birthtime
to a datetime object usingdatetime.datetime.fromtimestamp()
.
Example:
import os
import datetime
# Path to the file
path = r"/Users/myfiles/sample.txt"
# get file creation time on mac
stat = os.stat(path)
c_timestamp = stat.st_birthtime
c_time = datetime.datetime.fromtimestamp(c_timestamp)
print(c_time)
Pathlib Module to Get the Creation and Modification Datetime of a file
From Python 3.4 onwards, we can use the pathlib module, which provides a wrapper for most OS functions. This module offers classes representing filesystem paths with semantics appropriate for different operating systems.
Let’s see how to use the pathlib module and datetime module to get the creation and modification Datetime of a file in Python.
Import pathlib module and datetime module:
- Pathlib module offers classes and methods to handle filesystem paths and get data related to files for different operating systems.
- DateTime module used to convert the creation and modification time returned by the pathlib module to human-readable format (dd-mm-YYYY HH:MM:SS).
Use pathlib.Path(‘file path’)
to construct a file path
The absolute or relative path of a file. Use pathlib.Path()
class to create a concrete path (location of the file) of the system’s path flavor. it will return the file path object.
It is a cross-platform implementation. For example, If you execute this on windows, you will get the instance of class ‘pathlib.WindowsPath.’
Use the stat() method of a pathlib object
To get the creation and modification time of a file, use the stat(
) method of a pathlib object. This method returns the metadata and various information related to a file, such as file size, creation, and modification time.
- Use the
stat().st_mtime()
to get the last content modification time in seconds stat().st_ctime
(Platform dependent):- the time of most recent metadata changes on Unix,
- the time of creation on Windows is expressed in seconds.
Wrap creation and modification time in a datetime object.
The datetime returned by the st_mtime()
and st_ctime()
are in a numeric timestamp format. Use the fromtimestamp()
method to format it in a human-readable format (dd-mm-YYYY HH:MM:SS)
Example
import datetime
import pathlib
# create a file path
f_name = pathlib.Path(r'E:\demos\oop_demos\test.txt')
# get modification time
m_timestamp = f_name.stat().st_mtime
# convert ti to dd-mm-yyyy hh:mm:ss
m_time = datetime.datetime.fromtimestamp(m_timestamp)
print(m_time)
# get creation time on windows
c_timestamp = f_name.stat().st_ctime
c_time = datetime.datetime.fromtimestamp(c_timestamp)
print(c_time)
Output:
2021-12-24 13:35:41.257598 2021-12-24 13:35:41.257598