Get File Creation and Modification Dates in Python



There are various ways to get the file creation and modification datetime in Python. We will use different methods from the OS and pathlib module to get the file creation and modification datetime in Python.

Using the OS Module

The OS module is a built-in module in Python, so you do not need to install it separately. It is used to interact with the operating system and perform various tasks such as file manipulation, process management, and environment variable management.

File Creation Time On Windows

Here we have used the OS module to find the creation time of a file. Initially, we need to import the os module and the datetime module.

The os module is used for getting the timestamp, whereas the datetime module is used for creating a datetime object.

The os.path.getctime('path') function is used to get the creation time of a file. The os.path.getctime('path') returns the creation time in a numeric timestamp in float.

Example

In the following example code, we retrieve the creation time in timestamp format, and then we use the datetime.fromtimestamp() to create a datetime object.

import datetime
import os
path = r"C:\Examples\samplefile.txt"
create_time = os.path.getctime(path)
print(create_time)
create_date = datetime.datetime.fromtimestamp(create_time)
print('Created on:', create_date)

When you run the program, it will show this output -

1652690657.7901006
Created on: 2022-05-16 14:14:17.790101

File Modification Time On Windows

Here we find the last modified time of the file using the OS module. Initially, we need to import the OS module and the datetime module. The OS module is used for getting the timestamp, whereas the datetime module is used for creating a datetime object.

We use the os.path.getmtime('path') function to get the last modified time of that file. The os.path.getmtime(?path') returns the modification time in the numeric timestamp. Then we convert this timestamp to a datetime object using the datetime.fromtimestamp() function.

Example

import datetime
import os
path = r"C:\Examples\samplefile.txt"
modify_time = os.path.getmtime(path)
print(modify_time)
modify_date = datetime.datetime.fromtimestamp(modify_time)
print('Modified on:', modify_date)

After running the program, you will get this result -

1652690891.8609138
Modified on: 2022-05-16 14:18:11.860914

File Modification Time On MAC AND UNIX

Here, we find the last modified time of the file using the OS module. Initially, we need to import the OS module and the datetime module.

The OS module is used for getting the timestamp, whereas the datetime module is used for creating a datetime object.

We use the os.path.getmtime('path') function to get the last modified time of that file. The os.path.getmtime('path') returns the modification time in the numeric timestamp. Then we convert this timestamp to a datetime object using the datetime.fromtimestamp() function.

Example 1

import datetime
import os
path = r"C:\Examples\samplefile.txt"
modify_time = os.path.getmtime(path)
print(modify_time)
modify_date = datetime.datetime.fromtimestamp(modify_time)
print('Modified on:', modify_date)

Output

This output will be displayed when the program runs ?

1652690891.8609138
Modified on: 2022-05-16 14:18:11.860914

Example 2

Here, we get the file creation time on Mac and Unix systems by using the OS and datetime module.

The OS module is used for getting the timestamp, whereas the datetime module is used for creating a datetime object.

We use the st_birthtime attribute from the os.stat() function to get the creation time of the file. This returns a numeric timestamp, which is converted into a datetime object by using the datetime.fromtimestamp() function..

In this example, we will understand how to get the timestamp of file creation on MAC and UNIX systems.

import os
import datetime
path = r"C:\Examples\samplefile.txt"
stat = os.stat(path)
create_timestamp = stat.st_birthtime
print(create_timestamp)
create_time = datetime.datetime.fromtimestamp(create_timestamp)
print(create_time)

You will see this result after executing the program -

1652690657.7901006
Created on: 2022-05-16 14:14:17.790101

Using Pathlib Module

The Pathlib module in Python is a standard library module that provides an object-oriented interface for working with file system paths.

It allows you to create, manipulate, and interact with file paths in a more simple way compared to traditional string manipulation methods. The Pathlib module is available in Python 3.4 and later versions.

File Creation Time On Windows

Here we use the pathlib module to get the creation time of a file. We initially import the pathlib and datetime modules.

The pathlib module is used for getting the timestamp, whereas the datetime module is used for creating a datetime object.

The pathlib.Path() is used to create a path to the file and returns the file path object. We use the st_ctime attribute from the stat() method to get the creation time of the file.

This returns a numeric timestamp, which is converted into a datetime object by using the datetime.fromtimestamp() function.

Example

To get the timestamp of file creation on Windows OS, the following program can be utilized.

import datetime
import pathlib
filename = pathlib.Path(r'C:\Examples\samplefile.txt')
create_timestamp = filename.stat().st_ctime
print(create_timestamp)
create_time = datetime.datetime.fromtimestamp(create_timestamp)
print(create_time)

The program gives this output when it is run -

1652690657.7901006
2022-05-16 14:14:17.790101

File Modification Time On Windows

Here we find the last modified time of the file using the pathlib module. Initially, we need to import the pathlib module and the datetime module. The pathlib module is used for getting the timestamp, whereas the datetime module is used for creating a datetime object.

We use the st_mtime attribute from the stat() method to get the last modified time of the file. This returns a numeric timestamp, which is converted into a datetime object by using the datetime.fromtimestamp() function.

Example

In this example, we will get the file modification time on a system.

import datetime
import pathlib
filename = pathlib.Path(r'C:\Examples\samplefile.txt')
modify_timestamp = filename.stat().st_mtime
print(modify_timestamp)
modify_date = datetime.datetime.fromtimestamp(modify_timestamp)
print('Modified on:', modify_date)

The output produced is shown.

1652690891.8609138
Modified on: 2022-05-16 14:18:11.860914
Updated on: 2025-06-16T12:17:24+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started