How to Get the Creation and Modification Date of a File Using Python

In Python, you can get the creation and modification date of a file using the os.path module and the os.stat() method. The getctime() function retrieves the file creation time, while the getmtime() function returns the last modification time. These timestamps are typically represented in seconds since the epoch, but you can format them into a readable date format using the datetime module.


Examples to Get the Creation and Modification Date of a File

1. Get the Creation and Modification Date Using os.path.getctime() and os.path.getmtime()

In this example, we will use os.path.getctime() to get the file creation time and os.path.getmtime() to get the file modification time. We will then format these timestamps into a human-readable format using the datetime.datetime.fromtimestamp() method.

The following screenshot contains some of the details of example.txt.

main.py

</>
Copy
import os
import datetime

# Specify the file path
file_path = "example.txt"

# Get the creation and modification time
creation_time = os.path.getctime(file_path)
modification_time = os.path.getmtime(file_path)

# Convert timestamps to human-readable format
creation_date = datetime.datetime.fromtimestamp(creation_time)
modification_date = datetime.datetime.fromtimestamp(modification_time)

# Print the results
print("File Creation Date:", creation_date)
print("File Modification Date:", modification_date)

Explanation:

  1. We import the os module to access file system properties and datetime to format the output.
  2. The variable file_path stores the path of the file whose timestamps we want to retrieve.
  3. The os.path.getctime(file_path) function gets the creation time in seconds since the epoch.
  4. The os.path.getmtime(file_path) function gets the modification time in seconds since the epoch.
  5. The datetime.datetime.fromtimestamp() function converts these timestamps into human-readable date and time format.

Output:

2. Get File Timestamps Using os.stat()

In this example, we use the os.stat() method, which provides detailed file metadata, including the creation and modification timestamps.

main.py

</>
Copy
import os
import datetime

# Specify the file path
file_path = "example.txt"

# Get file stats
file_stats = os.stat(file_path)

# Extract creation and modification timestamps
creation_time = file_stats.st_ctime
modification_time = file_stats.st_mtime

# Convert timestamps to human-readable format
creation_date = datetime.datetime.fromtimestamp(creation_time)
modification_date = datetime.datetime.fromtimestamp(modification_time)

# Print the results
print("File Creation Date:", creation_date)
print("File Modification Date:", modification_date)

Explanation:

  1. We import the necessary modules: os for file metadata and datetime for formatting timestamps.
  2. The variable file_path stores the file’s location.
  3. The os.stat(file_path) function retrieves file metadata and stores it in file_stats.
  4. The file_stats.st_ctime gives the file’s creation time.
  5. The file_stats.st_mtime provides the last modification time.
  6. Both timestamps are converted into human-readable format using datetime.datetime.fromtimestamp().

Output:

3. Formatting File Timestamps in a Custom Format

In this example, we customize the date format of the file timestamps using strftime() for better readability.

main.py

</>
Copy
import os
import datetime

# Specify the file path
file_path = "example.txt"

# Get creation and modification time
creation_time = os.path.getctime(file_path)
modification_time = os.path.getmtime(file_path)

# Convert timestamps to custom format
custom_format = "%Y-%m-%d %H:%M:%S"
creation_date = datetime.datetime.fromtimestamp(creation_time).strftime(custom_format)
modification_date = datetime.datetime.fromtimestamp(modification_time).strftime(custom_format)

# Print the results
print("File Creation Date:", creation_date)
print("File Modification Date:", modification_date)

Explanation:

  1. We define a custom date-time format using the strftime() function.
  2. The format "%Y-%m-%d %H:%M:%S" displays timestamps in a more readable format (e.g., 2025-02-10 14:35:22).
  3. The strftime() method is applied to both timestamps to format them accordingly.

Output:

Conclusion

In summary, Python provides multiple ways to retrieve file creation and modification timestamps:

  1. os.path.getctime() and os.path.getmtime(): Simple methods to get creation and modification times.
  2. os.stat(): A comprehensive way to access file metadata.
  3. Custom Formatting: Using strftime() for better readability.