How To Get File Creation and Modification Date or Time in Python?
There are many ways to get the file creation and modification date and time in Python. Methods such as OS and pathlib module are used to get the file modification date and creation time in Python.
The os.path Module:
- os.path.getmtime(path): Os.path.getmtime(path) is the cross-platform that is used to set file modification time in Python. The Unix timestamp is returned by this method that tells last the file was modified.
- os.path.getctime('file_path'): Use to get the file creation time but for Windows only.
- os.stat(path).st_birthtime: To get file creation time on Mac and some Unix based systems. Use to get the file creation time on systems based on Unix and Mac.
The pathlib Module:
- pathlib.Path('file_path').stat().st_mtime: Used to get the file creation date and time in Python.
- pathlib.Path('file_path').stat().st_ctime: Used to get the file creation time in the window only and return the metadata of the file change time on Unix.
Steps to Get File Creation and Modification Date or Time using os.path Module
The steps show how to use the os.path module and datetime module to get the creation and modification time of the file.
Import os.path Module And datetime Module
The os.path module provides some functions based on pathnames. This function is used to get the meta data of the file.
The DateTime module converts the creation and modification time that is returned by the os.path module in the Day, year, months, hours, minutes, and seconds (dd-mm-YYYY HH:MM:SS) format.
Use the getmtime() Function to Get a Modification Time
The os.path.getmtime(‘file_path’) method is used to get the modification time numeric timestamp in float. The absolute or relative path of the file is passed to the method.
Wrap Creation and Modification Time in a Datetime Object.
The getmtime() method returns the time in a numeric timestamp format and the fromtimestamp() method of the datetime to format the date and time in dd-mm-YYYY HH:MM:SS.
Use the getctime() Function to Get a Creation Time
The os.path.getmtime('file_path') in Windows returns the creation time of the file but does not work well in Unix.
Example to Get File Modification and Creation Time
Code:
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
Explanation:
In the above code, the datetime and the os module is opened with location of the file and the getmtime method is used that is provided by the os to get the creation or modification data of the file. The time is converted to datetime object the result is printed. The getctime is used to get the creation time of the file and this time is then converted to the datetime object and the time is printed.
Get File Creation Datetime on Mac and Unix Systems
Code:
import os
import datetime
# Path to the file
path = "file.txt"
# Get file creation time on macOS
try:
c_timestamp = os.path.getctime(path)
c_time = datetime.datetime.fromtimestamp(c_timestamp)
print("File creation time:", c_time)
except OSError as e:
print(f"Error: {e}")
Output:
File creation time: 2024-01-19 22:05:59.725528
Explanation:
In the above code, the OS and the datetime module are imported for getting the creation and modification time of the file. The path of the file is input, and the stat of the file is extracted with the stat method and the path of the file is passed to the stat method and the creation time of the file is found with the help of the getctime method in which the path of the file is passed then this time is converted to the current time and the file creation time is printed. If there is any OSError, then the error is printed in the except block.
Using pathlib Module to Get the Creation and Modification Datetime of a file:
If you are using Python version 3.4 and later, you can use Pathlib module to get the creation or modification time of the file in Python, this pathlib module provides a wrapper function for many OS functions. There are many classes offered by this module that represent filesystem paths with semantics that are appropriate for different OS. Let's see how the Pathlib module is used to get the creation or modification date of the file and the datetime module in Python.
Import pathlib Module and datetime Module:
- The pathlib module in Python provides classes and methods that handle the path of the file system and get related files for different operation systems.
- The dateTime module is used to convert the creation or modification time that is returned by the pathlib module to a human readable format (dd-mm-YYYY HH:MM:SS)
Use Pathlib.Path(‘File Path’) To Make a File Path:
With the help of the pathlib.Path() class that is provided by the Pathlib module to create a location of the file of the system’s path that returns the file path object. This class is cross-platform implementation. For example, if you want to run this class on the window, an instance of this class is returned.
Use The Stat() Method Of A Pathlib Object:
To get the creation or modification time of the file in Python, you can use the stat() method of the Pathlib object. The stat() method of the Pathlib object returns the metadata and the related information of the file, such as size of the file, creation, and modification time.
- The stat().st_mtime() can be used to get the last content modification time in seconds.
- Use the stat().st_ctime that is platform independent:
- The time this is the most recent change time in the metadata on Unix.
- The creation of the file on Windows is expressed in seconds.
Wrap Creation and Modification Time in a Datetime Object:
The st_mtime() and st_ctime() method returns the datetime that is in a numeric timestamp format. The method fromtimestamp() can be used to format the timestamp in a huma-readable fomat (dd-mm-YYYY HH:MM:SS). Let's see a code on how to get the creation and modification time of the file with the help of the Pathlib object.
Code:
import datetime
import pathlib
# create a file path
f_name = pathlib.Path(r'file.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:
2024-01-19 22:05:59.725528
2024-01-19 22:05:59.725528
Explanation:
In the above code, the datetime and the Pathlib module are imported, the path of the file is input and the modification datetime of the file is extracted with the help of the stat().st_mtime() method and this time is converted to the datetime and the creation time of the file is extracted with the help of the st_ctime() method of the Patlib object the this time is converted to human-readable format and the creation time is printed.