Last modified time of a file

EasyOS Module Programs · 45 of 49

Write a Python program to get the last modified time of a file.

Advertisement
Output
True

Approach

  1. os.path.getmtime.

Solution

Python
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.

More os module programs

← All 49 OS Module Programs

Leave a Comment