Python Practice › OS Module Programs › Program 44
Creation (change) time of a file
Write a Python program to get the ctime of a file with os.path.getctime().
Output
float True
Approach
- The result is a timestamp; format it with
datetime.
Solution
import osfrom datetime import datetime open("timed.txt", "w").close()ctime = os.path.getctime("timed.txt")print(type(ctime).__name__)print(datetime.fromtimestamp(ctime).date() == datetime.now().date())Output
float True
Note
On Windows ctime is the creation time; on Linux it is the last metadata change.
Learn the concept
New to this topic? Read the lesson, then come back.