【问题标题】:Modify file create / access / write timestamp with python under windowswindows下用python修改文件创建/访问/写入时间戳
【发布时间】:2014-02-05 00:39:07
【问题描述】:

我试图找到一种在windows下使用python修改文件时间戳的简单方法,但是网上没有太多明确的信息。经过一段时间的搜索,我得到了解决方案。为了缩短对其他人的搜索,代码如下。

它可能会更简单、更美观,但它确实有效。我唯一没有解决的是夏季时间 - 冬季时间问题,即如果给出夏季时间,结果会相差一小时。也许有人可以添加更正?

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)

【问题讨论】:

标签: python windows file timestamp


【解决方案1】:

您可能需要在两个地方校正一小时的冬夏时差。在这两种情况下,我们都使用了tm_isdst 字段,time.localtime 可以方便地计算该字段来告诉我们夏令时 (DST) 是否对特定时间戳有效。

输入校正

如果您在夏季设置冬季时间戳,反之亦然,除非您在致电 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 生效后一年两次看到它们跳一个小时然后消失。

为什么夏令时如此不一致?

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

【讨论】:

  • 如果您使用诸如 POSIX 时间戳(Python 所期望的)、FILETIME(Windows,you can easily convert one into another)等时间戳来表达您的时间,则没有 DST 转换——注意:没有 DST 调整 )。如果函数需要本地时间作为输入;如果您需要以某种方式对其进行解释,则它会被设计破坏,即显示当地时间是可以的,但您不应该尝试用它进行简单的算术运算。
【解决方案2】:

使用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

【讨论】:

  • 嗯,是的,有一种简单的方法可以更改 atime 和 mtime,但不能更改 ctime。这就是我采取hard方式的原因:)
  • 我想用一个日期时间来设置mtime,所以我不得不这样做:t = time.mktime(d.timetuple())
  • @Pat,如果你使用 Python 3.3+,你可以使用 [datetime.datetime.timestamp](docs.python.org/3/library/…):t = d.timestamp()
  • 谢谢!我在这个项目中是 2.7,但很高兴知道更现代的方式。
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多