76

Assuming the file exists (using os.path.exists(filename) 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.

0

3 Answers 3

143
>>> import os
>>> f = os.path.getmtime('test1.jpg')
>>> f
1223995325.0

since the beginning of (epoch)

Sign up to request clarification or add additional context in comments.

Comments

76

os.stat()

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

Comments

57

New for python 3.4+ (see: pathlib)

import pathlib

path = pathlib.Path('some/path/to/file.ext')
last_modified = path.stat().st_mtime

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.