Python Practice › OS Module Programs › Program 45
Last modified time of a file
Write a Python program to get the last modified time of a file.
Output
True
Approach
os.path.getmtime.
Solution
import osimport time open("timed.txt", "w").close()before = os.path.getmtime("timed.txt")time.sleep(0.05)with open("timed.txt", "a") as f: f.write("more")print(os.path.getmtime("timed.txt") >= before)Output
True
Line by line
Lines 6-8
Writing to the file updates its modified time.
Learn the concept
New to this topic? Read the lesson, then come back.