Q: finding distance between 2 time's

John Machin sjmachin at lexicon.net
Sat May 30 20:49:39 EDT 2009


On May 31, 7:37 am, jkv <j... at unixcluster.dk> wrote:
> mar... at hvidberg.net wrote:
> > Thanks both
>
> > The first answer is quite instuctive, the other one might be the one
> > I'll use in t
>
> I didn't receive the other answer, could you please forward it to me?> So 2x thanks.
>
> You are welcome.
>
> I took another look at your code, and you can compress it all to a if
> "oneliner":
> (and thanks to Steven for the <= reminder)
>
>         if os.path.isfile(x):
>             nSize = os.path.getsize(x)
>             #if oneliner
>             if time.mktime(time.localtime()) -
> time.mktime(time.localtime(os.path.getmtime(x))) <= 3600:
>               print ('HT fil -> %s %d @ %s') % (x, nSize, time.asctime)

There is no virtue in onelinedness if the code is unclear and/or
incorrect.

Problems:
(1) Converting UTC time to local time and back again??

  > import time
  You are in a maze of twisty little functions, all alike.

So it can pay to do things a step at a time, and leave a note of your
intentions at each step.

(2) Consider grabbing the "current time" once instead of each for each
file

(3) asctime ... you are missing (an_argument)

import os, time, sys

def buildList(directory):
    listing = os.listdir(directory)
    now_utc_secs = time.time()
    for x in os.listdir(directory):
        x = os.path.join(directory, x)
        if os.path.isdir(x):
            print ('dir -> %s') % x
        elif os.path.isfile(x):
            mtime_utc_secs = os.path.getmtime(x)
            if now_utc_secs - mtime_utc_secs <= 3600.0:
                nSize = os.path.getsize(x)
                mtime_local_tuple = time.localtime(mtime_utc_secs)
                mtime_local_text = time.asctime(mtime_local_tuple)
                print ('fil -> %s %d @ %s') % (x, nSize,
mtime_local_text)

tstN = time.localtime()
print tstN
print "Time now: %s" % time.asctime(tstN)
buildList(sys.argv[1])

HTH,
John



More information about the Python-list mailing list