Get last modified date of a directory (including subdirectories) using Python?
I am trying to get the latest date and time of a directory change. with that, I want to include the last modified date of the subdirectories too.
I could find some threads related to this question. ( How do I get file creation and modified date / time in Python? ), But they all just give the last modified root time, excluding subdirectories.
import os.path, time
print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %s" % time.ctime(os.path.getctime(file))
These lines of code simply give the last modified time of the root directory, excluding subdirectories. Please help me with this.
+3
source to share
1 answer
This should do what you ask:
import os
import time
print time.ctime(max(os.stat(root).st_mtime for root,_,_ in os.walk('/tmp/x')))
But I can see what you are using os.path.getmtime()
. So you're probably looking for this:
print time.ctime(max(os.path.getmtime(root) for root,_,_ in os.walk('/tmp/x')))
+9
source to share