我很难使用os.utime
来正确设置mac上的修改时间(MacOSX10.6.2,从/usr/bin/python
运行Python2.6.1)。它与touch
实用程序不一致,也与Finder的"get信息“窗口中显示的属性不一致。
考虑以下命令序列。纯文本中的“创建”和“修改”时间指的是查找器中的"get info“窗口中显示的属性。作为提醒,os.utime接受参数(filename, (atime, mtime))
。
>>> import os
>>> open('tempfile','w').close()
“创建”和“修改”都是当前时间。
>>> os.utime('tempfile', (1000000000, 1500000000) )
“创建”是当前的时间,“修改”是2017年7月13日。
>>> os.utime('tempfile', (1000000000, 1000000000) )
“创建”和“修改”都是2001年9月8日。
>>> os.path.getmtime('tempfile')
1000000000.0
>>> os.path.getctime('tempfile')
1269021939.0
>>> os.path.getatime('tempfile')
1269021951.0
...but os.path.get?time
和os.stat
没有反映这一点。
>>> os.utime('tempfile', (1500000000, 1000000000) )
“创建”和“修改”仍然是2001年9月8日。
>>> os.utime('tempfile', (1500000000, 1500000000) )
“创建”是2001年9月8日,“修改”是2017年7月13日。
我不确定这是Python问题还是Mac问题。退出Python并运行
touch -a -t 200011221234 tempfile
修改和创建时间都不会像预期的那样被更改。然后我跑了
touch -m -t 200011221234 tempfile
同时,“创建”和“修改”的时间都发生了变化。
有人知道这是怎么回事吗?如何在mac上一致地更改修改和创建时间?(是的,我知道在Unixy系统上没有“创建时间”)。
运行Chris Johnsen脚本的结果:
seth@local:~$ /usr/bin/python timetest.py tempfile 5
initial:
(1269631281.0, 1269631281.0, 1269631281.0, 1269631281, 1269631281, 1269631281)
test: (1000000000, 1000000000)
(1000000000.0, 1000000000.0, 1269631281.0, 1000000000, 1000000000, 1269631281)
(1269631281.0, 1000000000.0, 1269631281.0, 1269631281, 1000000000, 1269631281)
test: (1000000000, 1500000000)
(1000000000.0, 1500000000.0, 1269631286.0, 1000000000, 1500000000, 1269631286)
(1269631286.0, 1500000000.0, 1269631286.0, 1269631286, 1500000000, 1269631286)
test: (1500000000, 1000000000)
(1500000000.0, 1000000000.0, 1269631291.0, 1500000000, 1000000000, 1269631291)
(1269631291.0, 1000000000.0, 1269631291.0, 1269631291, 1000000000, 1269631291)
test: (1500000000, 1500000000)
(1500000000.0, 1500000000.0, 1269631296.0, 1500000000, 1500000000, 1269631296)
(1269631296.0, 1500000000.0, 1269631296.0, 1269631296, 1500000000, 1269631296)
在练习结束时,在查找器中可以看到的“创建”日期是9/8/01,“修改”日期是7/13/17。(由于您的建议和我已经了解到的,访问日期大概是“现在”。)在查找器中可见的创建日期和修改日期仍然没有意义。
发布于 2010-03-26 18:52:30
时间、时间、时间
如果包含完整的脚本及其实际和预期的输出,而不是REPL片段,可能会有所帮助。
import sys, os, stat, time
def get_times(p):
s = os.stat(p)
return (
os.path.getatime(p),
os.path.getmtime(p),
os.path.getctime(p),
s[stat.ST_ATIME],
s[stat.ST_MTIME],
s[stat.ST_CTIME],
)
def main(p, delay=1):
delay = float(delay)
(a,b) = (1000000000, 1500000000)
open(p,'w').close()
print 'initial:'
print get_times(p)
for t in [ (a,a), (a,b), (b,a), (b,b) ]:
print
print 'test:', t
os.utime(p,t)
print get_times(p)
time.sleep(delay)
print get_times(p)
main(*sys.argv[1:])
我在我的10.4系统中使用了cd "$HOME" && python test.py tempfile 5
(系统默认的Python2.3.6和MacPorts Python2.6.4都给出了相同的结果(当然省略了最初的时间和时间):
% python /tmp/test.py tempfile 5
initial:
(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)
test: (1000000000, 1000000000)
(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)
(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)
test: (1000000000, 1500000000)
(1000000000.0, 1500000000.0, 1269629886.0, 1000000000, 1500000000, 1269629886)
(1000000000.0, 1500000000.0, 1269629886.0, 1000000000, 1500000000, 1269629886)
test: (1500000000, 1000000000)
(1500000000.0, 1000000000.0, 1269629891.0, 1500000000, 1000000000, 1269629891)
(1500000000.0, 1000000000.0, 1269629891.0, 1500000000, 1000000000, 1269629891)
test: (1500000000, 1500000000)
(1500000000.0, 1500000000.0, 1269629896.0, 1500000000, 1500000000, 1269629896)
(1500000000.0, 1500000000.0, 1269629896.0, 1500000000, 1500000000, 1269629896)
这似乎是合理的。我想知道你得到了什么。
我听说Spotlight有时会因为重新索引更改的文件而积极地重置atime。我不希望它重新索引一个只经历了utime()/utimes()的文件,但我认为这是可能的。要消除Spotlight可能出现的复杂情况,请使用Spotlight没有索引的位置上的文件(例如/tmp/testfile)。
在Finder中创建的日期
(在Finder的Get Info窗口中显示为“Created:”)
如果安装了开发人员工具,可以使用/Developer/Tools/GetFileInfo
查看HFS creationDate。我在每个print get_times(p)
行之后添加了以下行:
sys.stdout.flush()
os.system('/Developer/Tools/GetFileInfo ' + p)
我还更改了迭代以匹配您的初始描述([ (a,b), (a,a), (b,a), (b,b) ]
)。
现在的结果如下:
% rm /tmp/tempfile; python /tmp/test.py /tmp/tempfile 1
initial:
(1269636574.0, 1269636574.0, 1269636574.0, 1269636574, 1269636574, 1269636574)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 03/26/2010 15:49:34
modified: 03/26/2010 15:49:34
test: (1000000000, 1500000000)
(1000000000.0, 1500000000.0, 1269636574.0, 1000000000, 1500000000, 1269636574)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 03/26/2010 15:49:34
modified: 07/13/2017 21:40:00
(1000000000.0, 1500000000.0, 1269636574.0, 1000000000, 1500000000, 1269636574)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 03/26/2010 15:49:34
modified: 07/13/2017 21:40:00
test: (1000000000, 1000000000)
(1000000000.0, 1000000000.0, 1269636576.0, 1000000000, 1000000000, 1269636576)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 09/08/2001 20:46:40
(1000000000.0, 1000000000.0, 1269636576.0, 1000000000, 1000000000, 1269636576)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 09/08/2001 20:46:40
test: (1500000000, 1000000000)
(1500000000.0, 1000000000.0, 1269636577.0, 1500000000, 1000000000, 1269636577)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 09/08/2001 20:46:40
(1500000000.0, 1000000000.0, 1269636577.0, 1500000000, 1000000000, 1269636577)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 09/08/2001 20:46:40
test: (1500000000, 1500000000)
(1500000000.0, 1500000000.0, 1269636578.0, 1500000000, 1500000000, 1269636578)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 07/13/2017 21:40:00
(1500000000.0, 1500000000.0, 1269636578.0, 1500000000, 1500000000, 1269636578)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 07/13/2017 21:40:00
这似乎与您在Finder中Get Info窗口中的观察结果一致。我的解释(其他实验证实)是,HFS creationDate是由utime更新的,但它只会倒退(从不向前)。如果要将HFS creationDate更新为较新的值,则可能需要使用特定于Mac的API进行更新。
另一个注意事项:您可能需要稍微切换一下窗口,才能更新get Info窗口。在我的系统上,除非我将窗口切换到或从Get Info窗口,否则它的显示不会自动更新。
https://stackoverflow.com/questions/2479690
复制相似问题