function to find the modification date of the project

MRAB google at
Mon Jan 19 23:50:30 CET 2009

Joe Strout wrote:
> This isn't a question, but something I thought others may find useful 
> (and if somebody can spot any errors with it, I'll be grateful).
> We had a case recently where the client was running an older version of 
> our app, and didn't realize it.  In other languages I've avoided this by 
> displaying the compile date in the About box, but of course Python 
> doesn't really have a meaningful compile date.  So, instead we're now 
> displaying the latest modification date of any .py file in the project. 
>  Here's the function that finds that:
> def lastModDate():
>     "Get the latest modification date (as a string) of any .py file in 
> this project."
>     import os, time
>     latest = 0
>     dir = os.path.dirname(__file__)
>     if dir == '': dir = '.'  # HACK, but appears necessary
>     for fname in os.listdir(dir):
>         if fname.endswith('.py'):
>             modtime = os.stat(os.path.join(dir, fname)).st_mtime
>             if modtime > latest: latest = modtime
>     out = time.strftime('%Y-%m-%d', time.localtime(latest))
>     return out
> The intent of this code is to find any .py files in the same directory 
> as the module containing the above code, and return (as a date string in 
> SQL/ISO format) the latest modification date thereof.  Then, this is 
> displayed to the user in the about box like so:
>         dlg = wx.MessageDialog(self,
>               "etown unified database system\nRevision: %s" \
>               % lastModDate(),
>               "About etown Central", wx.OK | wx.ICON_INFORMATION)
>         dlg.ShowModal()
>         dlg.Destroy()
> (That's wxPython, of course.)
> I haven't yet tested this in a packaged app or on Windows, but it seems 
> to work in our OS X test environment.
> One odd thing was the need to employ the HACK identified above, where if 
> __file__ happens to already be in the current directory, then 
> os.path.dirname of it returns the empty-string -- yet the empty-string 
> is not a valid argument to os.listdir().  Is there a better way to a 
> list of files in the same directory as a given file?
I tend to prefer:

     dir = os.path.dirname(sys.argv[0])


     modtime = os.path.getmtime(os.path.join(dir, fname))

More information about the Python-list mailing list