修改文件创build/访问/写在Windows下的Python与时间戳

我试图find一个简单的方法来修改窗口下的文件时间戳使用python,但没有太多明确的信息在networking上。 search一段时间后,我得到了一个解决scheme 为了缩短search他人,代码在这里。

这可能会做得更容易,更美丽,但它的作品。 我没有解决的唯一问题是夏天 – 冬天的时间问题,即如果在夏天的时候,结果相差一个小时。 也许有人可以添加更正?

from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandle from win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING from pywintypes import Time import time import sys import os if len(sys.argv)<5: pfile = os.path.basename(sys.argv[0]) print "USAGE:\n\t%s <createTime> <modifyTime> <accessTime> <FileName>\n" % pfile print "EXAMPLE:" print '%s "01.01.2000 00:00:00" "01.01.2000 00:00:00" "01.01.2000 00:00:00" file' % (pfile) sys.exit() # get arguments cTime = sys.argv[1] # create mTime = sys.argv[2] # modify aTime = sys.argv[3] # access fName = sys.argv[4] # specify time format format = "%d.%m.%Y %H:%M:%S" offset = 0 # in seconds # create struct_time object cTime_t = time.localtime(time.mktime(time.strptime(cTime,format))+offset) mTime_t = time.localtime(time.mktime(time.strptime(mTime,format))+offset) aTime_t = time.localtime(time.mktime(time.strptime(aTime,format))+offset) # visually check if conversion was ok print print "FileName: %s" % fName print "Create : %s --> %s OK" % (cTime,time.strftime(format,cTime_t)) print "Modify : %s --> %s OK" % (mTime,time.strftime(format,mTime_t)) print "Access : %s --> %s OK" % (aTime,time.strftime(format,aTime_t)) print # change timestamp of file fh = CreateFile(fName, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTime, accessTime, modifyTime = GetFileTime(fh) print "Change Create from",createTime,"to %s" % (time.strftime(format,cTime_t)) print "Change Modify from",modifyTime,"to %s" % (time.strftime(format,mTime_t)) print "Change Access from",accessTime,"to %s" % (time.strftime(format,aTime_t)) print createTime = Time(time.mktime(cTime_t)) accessTime = Time(time.mktime(aTime_t)) modifyTime = Time(time.mktime(mTime_t)) SetFileTime(fh, createTime, accessTime, modifyTime) CloseHandle(fh) # check if all was ok ctime = time.strftime(format,time.localtime(os.path.getctime(fName))) mtime = time.strftime(format,time.localtime(os.path.getmtime(fName))) atime = time.strftime(format,time.localtime(os.path.getatime(fName))) print "CHECK MODIFICATION:" print "FileName: %s" % fName print "Create : %s" % (ctime) print "Modify : %s" % (mtime) print "Access : %s" % (atime) 

使用os.utime ,你可以改变atime,mtime(无ctime)。

 >>> import time >>> import os >>> t = time.mktime(time.strptime('16.01.2014 00:00:00', '%d.%m.%Y %H:%M:%S')) >>> t 1389798000.0 >>> os.utime('..\path\to\file', (t,t)) # <--- >>> os.path.getmtime('..\path\to\file') 1389798000.0 

有两个地方你可能想纠正一个小时的冬季/夏季差异。 在这两种情况下,我们都使用tm_isdst字段,通过time.localtime方便地计算,以告诉我们夏令时(DST)是否对特定时间戳有效。

输入更正

如果您在夏季设置冬季时间戳,反之亦然,则在匹配季节到来时,它将会SetFileTime一小时,除非您在调用SetFileTime之前进行补偿:

 now = time.localtime() createTime = Time(time.mktime(cTime_t) + 3600 * (now.tm_isdst - cTime_t.tm_isdst)) accessTime = Time(time.mktime(aTime_t) + 3600 * (now.tm_isdst - aTime_t.tm_isdst)) modifyTime = Time(time.mktime(mTime_t) + 3600 * (now.tm_isdst - mTime_t.tm_isdst)) SetFileTime(fh, createTime, accessTime, modifyTime) 

输出校正

要使Python报告与Windows资源管理器匹配,我们在调用strftime之前应用更正:

 # check if all was ok now = time.localtime() ctime = os.path.getctime(fName) mtime = os.path.getmtime(fName) atime = os.path.getatime(fName) ctime += 3600 * (now.tm_isdst - time.localtime(ctime).tm_isdst) mtime += 3600 * (now.tm_isdst - time.localtime(mtime).tm_isdst) atime += 3600 * (now.tm_isdst - time.localtime(atime).tm_isdst) ctime = time.strftime(format,time.localtime(ctime)) mtime = time.strftime(format,time.localtime(mtime)) atime = time.strftime(format,time.localtime(atime)) 

两个更正

要小心,如果你同时应用,你的Python输出将再次看起来不匹配你的输入。 这可能是可取的(见下文),但如果它困扰你:

  • 如果您更喜欢时间戳在一年的本地时间看,只选择输入更正
  • 如果您习惯于在DST生效后每年两次跳一小时,请选择“ 输出修正” ,然后消失。

为什么DST如此不一致?

Python和Windows选择了不同的方法来转换UTC和本地时区之间的时间戳:

  • Python使用时间戳中有效的DST代码。 这样,时间戳就会全年一致的表示。

  • Windows现在使用DST代码生效。 这样,所有显示的时间戳都有相同的隐含代码。

如果使用'%Z'在转换的字符串中包含时区(例如,PST与PDT),但是由于大多数应用程序(包括Windows资源管理器)都不显示明显的一小时不一致性,这一点很明显。

当用明确的时间码打印时,很明显,每一栏中的邮票确实都代表同一时刻:

 File #1 (January) File #2 (June) 2000-01-30 20:00:00 UTC 2000-06-22 20:00:00 UTC observed in January in California: 2000-01-30 12:00:00 PST 2000-06-30 13:00:00 PDT [Python] 2000-01-30 12:00:00 PST 2000-06-30 12:00:00 PST [Windows] observed in June in California: 2000-01-30 12:00:00 PST 2000-06-30 13:00:00 PDT [Python] 2000-01-30 13:00:00 PDT 2000-06-30 13:00:00 PDT [Windows] observed in June in New York: 2000-01-30 15:00:00 EST 2000-06-30 16:00:00 EDT [Python] 2000-01-30 16:00:00 EDT 2000-06-30 16:00:00 EDT [Windows] 

如果我们可以要求strftime遵守tm_isdst字段,以匹配Windows资源管理器和大多数显示文件时间戳的应用程序,但是至少有一个简单的解决方法可以自己进行计算,那将是非常好的。

 def adjustForDST (seconds): now = time.localtime() correction = 60*60 * (now.tm_isdst - time.localtime(seconds).tm_isdst) return seconds + correction time.strftime(format, time.localtime(adjustforDST(mtime))) 

资料来源:

http://bytes.com/topic/python/answers/655606-python-2-5-1-broken-os-stat-module http://search.cpan.org/~shay/Win32-UTCFileTime-1.58/ LIB /的Win32 / UTCFileTime.pm

如果cpan链接再次出现新的修订版本,请按照以下方式找到它:

https://www.google.com/search?q=UTCFileTime.pm