Bill the Lizard Bill the Lizard - 3 years ago 165
Python Question

How do I get the time a file was last modified in Python?

Assuming the file exists (using

to first make sure that it does), how do I display the time a file was last modified? This is on Linux if that makes any difference.

Answer Source


import os
filename = "/etc/fstab"
statbuf = os.stat(filename)
print("Modification time: {}".format(statbuf.st_mtime))

Linux does not record the creation time of a file (for most fileystems).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download