time: Daylight savings confusion

Tim H tim--at-- at frontier--dot--.net
Thu Feb 5 01:35:13 EST 2009


On Win XP 64bit, Python 2.6.1 64bit

I am trying to rename files by their creation time.

It seems the time module is too smart for its own good here.

time.localtime(os.path.getctime(f)) returns a value one hour off from 
what windows reports for files that were created when Daylight savings 
time was in effect (ie when the tm_isdst field is one).  I can kludge 
it, but am I missing the "right" way to do it?

Tim

full code:
import os, time

filetypes = (".avi")

skipped = []
for f in os.listdir("."):
     root, ext = os.path.splitext(f)
     if ext in filetypes:
         creation_time = time.localtime(os.path.getctime(f))
         newname = time.strftime("%Y-%m-%d_%H%M" + ext, creation_time)
         if os.path.exists(newname):
             skipped.append(newname)
         else:
             os.rename(f, newname)
             print f, "->", newname
     else:
         skipped.append(f)

print
print "These files were skipped:"
for sf in skipped:
     print sf



More information about the Python-list mailing list