問題描述
我試圖找到一種在windows下使用python修改文件時間戳的簡單方法,但是網上沒有太多明確的信息.經過一段時間的搜索,我得到了解決方案.為了縮短對其他人的搜索,代碼如下.
I tried to find an easy way to modifiy a file timestamp under windows using python, but there was not much clear information on the web. After searching a while I got a solution. To shorten the search for others, the code follows here.
它可能會更簡單、更美觀,但它確實有效.我唯一沒有解決的是夏季時間 - 冬季時間問題,即如果給出夏季時間,結果會相差一小時.也許有人可以添加更正?
It might be done easier and more beautiful, but it works. The only thing I didn't solve is the summer time - winter time issue, i.e. if a time in summer is given, the result differs by one hour. Maybe someone can add a correction?
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:
%s <createTime> <modifyTime> <accessTime> <FileName>
" % 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)
推薦答案
您可能需要在兩個地方更正冬/夏一小時的差異.在這兩種情況下,我們都使用 tm_isdst
字段,time.localtime
可以方便地計算出 夏令時 (DST)對特定時間戳有效.
There are two places where you might want to correct for winter/summer difference of one hour. In both cases, we make use of the tm_isdst
field, which time.localtime
conveniently calculates to tell us whether Daylight Savings Time (DST) was in effect for a particular timestamp.
如果您在夏季設置冬季時間戳,反之亦然,除非您在調用 SetFileTime
之前進行補償,否則它將在其匹配季節到來時關閉一個小時:
If you are setting a winter timestamp during summer, or vice versa, it will become off by an hour when its matching season comes around unless you compensate before calling 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 輸出將再次與您的輸入不匹配.這可能是可取的(見下文),但如果它困擾您:
Both Corrections
Beware, if you apply both, your Python output will again seem to mismatch your input. This may be desirable (see below), but if it bothers you:
- 如果您更喜歡與一年中的原始時間相同的時間戳,請僅選擇 輸入更正.
- 僅選擇輸出校正,如果您習慣于在 DST 生效后一年兩次看到它們跳一小時然后消失.
- Choose only Input Correction if you prefer timestamps that look right at their native time of year.
- Choose only Output Correction if you're used to seeing them jump an hour twice a year as DST takes effect and then goes away.
Python 和 Windows 選擇了不同的方法在 UTC 和本地時區之間轉換時間戳:
Python and Windows have chosen different methods to convert timestamps between UTC and the local time zone:
Python 使用在時間戳有效的 DST 代碼.這樣,時間戳全年都有一致的表示.
Python uses the DST code that was in effect at the timestamp. This way, the time stamp has a consistent representation year-round.
Windows 使用現在生效的 DST 代碼.這樣,顯示的所有時間戳都具有相同的隱式代碼.
Windows uses the DST code in effect right now. This way, all time stamps shown have the same implicit code.
如果您使用 '%Z' 在轉換后的字符串中包含時區(例如 PST 與 PDT),這一點很明顯,但由于大多數應用程序(包括 Windows 資源管理器)不這樣做,明顯的一小時不一致可能清單.
This is evident if you use '%Z' to include the time zone in the converted string (PST vs. PDT, for example) but since most apps (including Windows Explorer) do not, an apparent one-hour inconsistency can manifest.
當使用明確的時間碼打印時,很明顯每列中的標記確實都代表同一時間:
When printed with explicit time codes, it becomes clear that the stamps in each column really do all represent the same instant in time:
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 資源管理器和大多數其他顯示文件時間戳的應用程序,那就太好了,但至少有一個簡單的解決方法可以自己進行計算.
It would be nice if we could ask strftime to honor the tm_isdst field, to match Windows Explorer and most other apps that display file timestamps, but at least there's a simple workaround to do the calculation ourselves.
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-modulehttp://search.cpan.org/~shay/Win32-UTCFileTime-1.58/lib/Win32/UTCFileTime.pm
如果 cpan 鏈接再次因新修訂而中斷,請按以下方式查找:
If the cpan link breaks again with a new revision, find it this way:
https://www.google.com/search?q=UTCFileTime.pm
這篇關于windows下用python修改文件創建/訪問/寫入時間戳的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!