Last active
January 2, 2017 00:10
-
-
Save jbarham/3375913 to your computer and use it in GitHub Desktop.
Returns timestamp of most recently modified file under given path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, os, time | |
def newest_mtime(path): | |
"""Returns timestamp of newest file under path.""" | |
newest_time = 0 | |
for root, dirs, files in os.walk(path): | |
for fname in files: | |
mtime = os.stat(os.path.join(root, fname)).st_mtime | |
if mtime > newest_time: | |
newest_time = mtime | |
return newest_time | |
def newest_timestamp(path, format="%Y%m%d%H%M"): | |
return time.strftime(format, time.localtime(newest_mtime(path))) | |
if __name__ == "__main__": | |
print(newest_timestamp(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script returns the timestamp of the file that has been changed most recently under the given directory path. It's useful to e.g. generate a URL timestamp for a website's static file root directory.
Use it in Django like so:
local_settings.py:
nginx.conf:
For more Django tips & tricks see my presentation at http://www.wombatsoftware.com/mpug2016.