How to Get File Creation Time in Python Easily
You can get a file's creation time in Python using
os.path.getctime(path) or pathlib.Path(path).stat().st_ctime. Note that on some systems like Linux, st_ctime may show the last metadata change time, not creation time.Syntax
Use os.path.getctime(path) or pathlib.Path(path).stat().st_ctime to get the creation time of a file.
path: The file path as a string orPathobject.getctime(): Returns the creation time as seconds since the epoch.stat().st_ctime: Accesses the creation time from file stats.
python
import os from pathlib import Path # Using os module creation_time = os.path.getctime('example.txt') # Using pathlib module file_path = Path('example.txt') creation_time_pathlib = file_path.stat().st_ctime
Example
This example shows how to get and print the creation time of a file in a readable format using os and pathlib. It converts the timestamp to a human-readable date and time.
python
import os from pathlib import Path import time file_name = 'example.txt' # Create the file for demonstration with open(file_name, 'w') as f: f.write('Hello!') # Get creation time using os ctime_os = os.path.getctime(file_name) print('Creation time using os:', time.ctime(ctime_os)) # Get creation time using pathlib file_path = Path(file_name) ctime_pathlib = file_path.stat().st_ctime print('Creation time using pathlib:', time.ctime(ctime_pathlib))
Output
Creation time using os: Thu Jun 8 12:00:00 2023
Creation time using pathlib: Thu Jun 8 12:00:00 2023
Common Pitfalls
On some operating systems like Linux, st_ctime does not return the file creation time but the last metadata change time. This can cause confusion if you expect the original creation time.
Windows reliably provides creation time, but Linux and macOS may not. For Linux, you might need third-party tools or extended attributes to get creation time.
Also, ensure the file exists before calling these functions to avoid errors.
python
import os file_name = 'nonexistent.txt' # Wrong: calling getctime on a missing file causes an error try: print(os.path.getctime(file_name)) except FileNotFoundError: print('File not found error caught!') # Right: check if file exists first if os.path.exists(file_name): print(os.path.getctime(file_name)) else: print('File does not exist.')
Output
File not found error caught!
File does not exist.
Quick Reference
| Function/Method | Description | Notes |
|---|---|---|
| os.path.getctime(path) | Returns file creation time (seconds since epoch) | Reliable on Windows; on Linux shows metadata change time |
| pathlib.Path(path).stat().st_ctime | Access creation time from file stats | Same behavior as os.path.getctime |
| time.ctime(timestamp) | Converts timestamp to readable string | Use to display creation time nicely |
Key Takeaways
Use os.path.getctime() or pathlib.Path().stat().st_ctime to get file creation time in Python.
On Windows, these methods return actual creation time; on Linux/macOS, they may return metadata change time.
Always check if the file exists before getting its creation time to avoid errors.
Convert the timestamp to a readable format with time.ctime() for easier understanding.
For true creation time on Linux, consider platform-specific tools or extended attributes.