Python os.path.getctime() Method

The os.path.getctime() method is used to retrieve the creation time of a file or directory. The time is expressed in seconds since the epoch (January 1, 1970, 00:00:00 UTC).




Name Description
path (string) It is a path of the file or directory.

Return value

It returns the creation time of the file or directory in seconds since the epoch.

  • On Windows, the getctime() method returns the actual creation time of the file or directory.
  • On Unix and Linux systems, this may return the time of the last metadata change (not the creation time) due to the way these systems handle file metadata.

Example 1: Getting the creation time of a file

Understanding of Python os.path.getctime() Method

import os
import time

path = '/Users/krunallathiya/Desktop/Code/pythonenv/env/data.txt'

creation_time = os.path.getctime(path)

print("Creation time:", time.ctime(creation_time))


Creation time: Sun Feb 4 22:01:03 2024

Example 2: Handling Non-Existent File

Handling Non-Existent File

import os

path = '/Users/krunallathiya/Desktop/Code/pythonenv/env/non_existent.txt'

  creation_time = os.path.getctime(path)
  print("Creation time:", creation_time)
except FileNotFoundError:
  print("File not found.")


File not found.

Be mindful of the differences in how this time is interpreted across different operating systems, especially when writing cross-platform code.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.