get file modification time in mm/dd/yyyy format?

Discussion in 'Python' started by beliavsky@aol.com, May 7, 2005.

  1. Guest

    Using Python 2.4 on Windows, for me the command

    print os.stat("temp.txt")[stat.ST_MTIME]

    gives

    1115478343 ,

    which is "seconds since the epoch". How can I get the modification time
    in a format such as

    05/07/2005 11:05 AM

    as in Windows with the dir command? Ideal would be a tuple of 6 values,
    (year,month,day,hour,minute,second). Thanks.
    , May 7, 2005
    #1
    1. Advertising

  2. Peter Hansen Guest

    wrote:
    > Using Python 2.4 on Windows, for me the command
    >
    > print os.stat("temp.txt")[stat.ST_MTIME]
    >
    > gives
    >
    > 1115478343 ,
    >
    > which is "seconds since the epoch". How can I get the modification time
    > in a format such as
    >
    > 05/07/2005 11:05 AM
    >
    > as in Windows with the dir command? Ideal would be a tuple of 6 values,
    > (year,month,day,hour,minute,second). Thanks.


    Do you want the time formatted as above, or as a tuple, since you seem
    unclear?

    time.strftime('%m/%d/%Y %H:%M %p', time.localtime(time.time()))

    time.localtime(time.time())[:6] would do the latter...

    Check the "time" module docs for more.

    -Peter
    Peter Hansen, May 7, 2005
    #2
    1. Advertising

  3. Guest

    Tried this it on linux, should work under windows as well I think
    martin@ubuntu:~$ python
    Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
    [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    py> import time
    py> t = time.localtime(1115478343)
    py> print t
    (2005, 5, 7, 17, 5, 43, 5, 127, 1)
    py> print time.asctime(t)
    Sat May 7 17:05:43 2005
    py>
    , May 7, 2005
    #3
  4. F. Petitjean Guest

    Le 7 May 2005 08:23:48 -0700, a écrit :
    > Using Python 2.4 on Windows, for me the command
    >
    > print os.stat("temp.txt")[stat.ST_MTIME]
    >
    > gives
    >
    > 1115478343 ,
    >
    > which is "seconds since the epoch". How can I get the modification time
    > in a format such as
    >
    > 05/07/2005 11:05 AM
    >
    > as in Windows with the dir command? Ideal would be a tuple of 6 values,
    > (year,month,day,hour,minute,second). Thanks.
    >

    I type
    import time
    dir(time)
    lot of stuff
    help(time.localtime)
    localtime([seconds]) ->
    (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)

    Convert seconds since the Epoch to a time tuple expressing local time.
    When 'seconds' is not passed in, convert the current time instead.

    >>> time.localtime(1115478343)

    (2005, 5, 7, 17, 5, 43, 5, 127, 1)
    it seems to be 7 may 2005 17h 5 minutes 43 seconds

    To have such a formatting you have to type help(time.strptime) which
    sadly will say
    See the library reference manual for formatting codes (same as strftime()).

    ---
    there is no perfect world
    F. Petitjean, May 7, 2005
    #4
    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. Abhijit
    Replies:
    8
    Views:
    15,640
    Matt Hurd
    Sep 16, 2004
  2. Chu
    Replies:
    3
    Views:
    4,167
    Shawn
    Aug 15, 2006
  3. Ashraf Ansari

    convert MM/dd/yyyy format into dd/MM/yyyy

    Ashraf Ansari, Aug 30, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    777
    Alexey Smirnov
    Aug 30, 2007
  4. Gilles Ganault

    Converting DD MM YYYY into YYYY-MM-DD?

    Gilles Ganault, Aug 17, 2009, in forum: Python
    Replies:
    18
    Views:
    1,330
    Mark Tolonen
    Aug 20, 2009
  5. J P Singh
    Replies:
    4
    Views:
    272
    J P Singh
    Feb 10, 2004
Loading...

Share This Page