find the creation date of a file in python

You can find the creation date of a file in Python using the os.path.getctime() function from the os module. This function returns the time of creation of the file as a Unix timestamp, which is the number of seconds since the epoch (January 1, 1970, 00:00:00 UTC).

Here's an example code snippet that demonstrates how to find the creation date of a file:

main.py
import os

filename = "example.txt"
creation_time = os.path.getctime(filename)

# Convert Unix timestamp to a readable date format
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(creation_time))

print(f"The file {filename} was created on {formatted_time}")
274 chars
10 lines

In this example, we first import the os module. Then, we specify the name of the file whose creation date we want to find in the filename variable. We use the os.path.getctime() function to get the time of creation of the file, and store it in the creation_time variable. Finally, we convert the Unix timestamp to a more readable date format using the time.strftime() function, and print out the result in a message.

Note that the time of creation of a file may not always be accurate, depending on the operating system and the file system being used.

gistlibby LogSnag