Python Program to Get File Creation and Modification Date


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co

Creation date and modification date are metadata that are associated with files and folders in a file system.

The creation date, also known as the “born on” date, is the date and time when a file or folder was originally created or added to a file system. It is a record of when the file or folder was first written to disk.

The modification date, also known as the “last modified” date, is the date and time when a file or folder was last modified or updated. It is a record of when the contents of the file or folder were last changed or edited.

These dates are important for managing files and folders, as they can help users keep track of when files were created or last modified


Python Code :

You can use the os module in Python to get the creation and modification date of a file. Here is an example program that demonstrates this:


import os
import datetime

filename = 'example.txt'

# Get the creation and modification times of the file
creation_time = os.path.getctime(filename)
modification_time = os.path.getmtime(filename)

# Convert the timestamp to a datetime object
creation_time = datetime.datetime.fromtimestamp(creation_time)
modification_time = datetime.datetime.fromtimestamp(modification_time)

# Print the results
print(f'File creation time: {creation_time}')
print(f'File modification time: {modification_time}')

In this program, we first import the os and datetime modules. We then specify the name of the file we want to get the creation and modification times for (in this example, example.txt). We use the os.path.getctime() and os.path.getmtime() functions to get the creation and modification times, respectively. These functions return a timestamp in seconds since the epoch (January 1, 1970).

We then use the datetime.datetime.fromtimestamp() function to convert the timestamps to datetime objects. Finally, we print the results.

Note that the creation time returned by os.path.getctime() may not be reliable on all platforms (e.g., on Unix systems, it may return the time of the last metadata change rather than the creation time).


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co