function to find the modification date of the project

Discussion in 'Python' started by Joe Strout, Jan 19, 2009.

  1. Joe Strout

    Joe Strout Guest

    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?

    Cheers,
    - Joe
    Joe Strout, Jan 19, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    565
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,185
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    492
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    691
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    384
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page