在mac上修改/创建/访问时间不一致

Set*_*son 6 python macos touch stat

我无法os.utime在mac上正确设置修改时间(Mac OS X 10.6.2,运行Python 2.6.1 /usr/bin/python).它与touch实用程序不一致,并且与Finder的"获取信息"窗口中显示的属性不一致.

请考虑以下命令序列.纯文本中的"已创建"和"已修改"时间是指查找程序中"获取信息"窗口中显示的属性.提醒一下,os.utime需要参数(filename, (atime, mtime)).

>>> import os
>>> open('tempfile','w').close()
Run Code Online (Sandbox Code Playgroud)

'created'和'modified'都是当前时间.

>>> os.utime('tempfile', (1000000000, 1500000000) )
Run Code Online (Sandbox Code Playgroud)

'created'是当前时间,'modified'是2017年7月13日.

>>> os.utime('tempfile', (1000000000, 1000000000) )
Run Code Online (Sandbox Code Playgroud)

"创造"和"修改"都是2001年9月8日.

>>> os.path.getmtime('tempfile')
1000000000.0
>>> os.path.getctime('tempfile')
1269021939.0
>>> os.path.getatime('tempfile')
1269021951.0
Run Code Online (Sandbox Code Playgroud)

......但是os.path.get?timeos.stat没有反映出来.

>>> os.utime('tempfile', (1500000000, 1000000000) )
Run Code Online (Sandbox Code Playgroud)

"创造"和"修改" 仍然是2001年9月8日.

>>> os.utime('tempfile', (1500000000, 1500000000) )
Run Code Online (Sandbox Code Playgroud)

'created'是2001年9月8日,'modified'是2017年7月13日.

我不确定这是Python问题还是Mac stat问题.当我退出Python shell并运行时

touch -a -t 200011221234 tempfile
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,修改和创建时间都没有改变.然后我跑了

touch -m -t 200011221234 tempfile
Run Code Online (Sandbox Code Playgroud)

并且"已创建"和"已修改"时间都已更改.

有谁知道发生了什么事?如何在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)
Run Code Online (Sandbox Code Playgroud)

在练习结束时,查找器中显示的"创建"日期是9/8/01,"修改"日期是7/13/17.(访问日期,多亏了你所建议的聚光灯,正如我所读到的,大致是'现在'.)在查找器中可见的创建和修改日期仍然没有意义.

Chr*_*sen 5

POSIX atime , mtime , ctime

\n\n

如果您包含完整的脚本及其实际和预期输出而不是 REPL 片段,这可能会有所帮助。

\n\n
import sys, os, stat, time\n\ndef get_times(p):\n    s = os.stat(p)\n    return ( \n        os.path.getatime(p),\n        os.path.getmtime(p),\n        os.path.getctime(p),\n        s[stat.ST_ATIME],\n        s[stat.ST_MTIME],\n        s[stat.ST_CTIME],\n    )\n\ndef main(p, delay=1):\n    delay = float(delay)\n    (a,b) = (1000000000, 1500000000)\n\n    open(p,\'w\').close()\n\n    print \'initial:\'\n    print get_times(p)\n\n    for t in [ (a,a), (a,b), (b,a), (b,b) ]:\n        print\n        print \'test:\', t\n        os.utime(p,t)\n        print get_times(p)\n        time.sleep(delay)\n        print get_times(p)\n\nmain(*sys.argv[1:])\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在 10.4 系统上得到了这个(系统默认的 Python 2.3.6 和 MacPorts Python 2.6.4 都给出了相同的结果(当然,cd "$HOME" && python test.py tempfile 5省略了初始时间和ctime )):

\n\n
% python /tmp/test.py tempfile 5\ninitial:\n(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)\n\ntest: (1000000000, 1000000000)\n(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)\n(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)\n\ntest: (1000000000, 1500000000)\n(1000000000.0, 1500000000.0, 1269629886.0, 1000000000, 1500000000, 1269629886)\n(1000000000.0, 1500000000.0, 1269629886.0, 1000000000, 1500000000, 1269629886)\n\ntest: (1500000000, 1000000000)\n(1500000000.0, 1000000000.0, 1269629891.0, 1500000000, 1000000000, 1269629891)\n(1500000000.0, 1000000000.0, 1269629891.0, 1500000000, 1000000000, 1269629891)\n\ntest: (1500000000, 1500000000)\n(1500000000.0, 1500000000.0, 1269629896.0, 1500000000, 1500000000, 1269629896)\n(1500000000.0, 1500000000.0, 1269629896.0, 1500000000, 1500000000, 1269629896)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这似乎很合理。我想知道你会得到什么。

\n\n

我听说 Spotlight 有时会由于重新索引更改的文件而主动重置时间。我不希望它重新索引仅经历过 utime()/utimes() 的文件,但我认为这是可能的。要消除 Spotlight 可能带来的麻烦,请使用位于 Spotlight 未索引的位置中的文件(例如 /tmp/testfile)。

\n\n

在Finder中创建的日期

\n\n

(在 Finder 的“获取信息”窗口中显示为 \xe2\x80\x9cCreated:\xe2\x80\x9d)

\n\n

如果您安装了开发人员工具,则可以使用它/Developer/Tools/GetFileInfo来查看 HFS 创建日期。我在每行后面添加了以下几行print get_times(p)

\n\n
sys.stdout.flush()\nos.system(\'/Developer/Tools/GetFileInfo \' + p)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我还更改了迭代以匹配您的初始描述 ( [ (a,b), (a,a), (b,a), (b,b) ])。

\n\n

现在的结果如下所示:

\n\n
% rm /tmp/tempfile; python /tmp/test.py /tmp/tempfile 1\ninitial:\n(1269636574.0, 1269636574.0, 1269636574.0, 1269636574, 1269636574, 1269636574)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 03/26/2010 15:49:34\nmodified: 03/26/2010 15:49:34\n\ntest: (1000000000, 1500000000)\n(1000000000.0, 1500000000.0, 1269636574.0, 1000000000, 1500000000, 1269636574)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 03/26/2010 15:49:34\nmodified: 07/13/2017 21:40:00\n(1000000000.0, 1500000000.0, 1269636574.0, 1000000000, 1500000000, 1269636574)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 03/26/2010 15:49:34\nmodified: 07/13/2017 21:40:00\n\ntest: (1000000000, 1000000000)\n(1000000000.0, 1000000000.0, 1269636576.0, 1000000000, 1000000000, 1269636576)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 09/08/2001 20:46:40\nmodified: 09/08/2001 20:46:40\n(1000000000.0, 1000000000.0, 1269636576.0, 1000000000, 1000000000, 1269636576)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 09/08/2001 20:46:40\nmodified: 09/08/2001 20:46:40\n\ntest: (1500000000, 1000000000)\n(1500000000.0, 1000000000.0, 1269636577.0, 1500000000, 1000000000, 1269636577)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 09/08/2001 20:46:40\nmodified: 09/08/2001 20:46:40\n(1500000000.0, 1000000000.0, 1269636577.0, 1500000000, 1000000000, 1269636577)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 09/08/2001 20:46:40\nmodified: 09/08/2001 20:46:40\n\ntest: (1500000000, 1500000000)\n(1500000000.0, 1500000000.0, 1269636578.0, 1500000000, 1500000000, 1269636578)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 09/08/2001 20:46:40\nmodified: 07/13/2017 21:40:00\n(1500000000.0, 1500000000.0, 1269636578.0, 1500000000, 1500000000, 1269636578)\nfile: "/private/tmp/tempfile"\ntype: ""\ncreator: ""\nattributes: avbstclinmedz\ncreated: 09/08/2001 20:46:40\nmodified: 07/13/2017 21:40:00\n
Run Code Online (Sandbox Code Playgroud)\n\n

这似乎与您在Finder中的“获取信息”窗口中观察到的结果一致。我的解释(由其他实验证实)是 HFS 创建日期由 utime 更新,但它只会向后(从不向前)。如果您想将 HFS CreationDate 更新为更新的值,那么您可能必须使用特定于 Mac 的 API 来完成此操作。

\n\n

另请注意:您可能需要稍微切换窗口才能更新“获取信息”窗口。在我的系统上,除非我在“获取信息”窗口之间切换窗口,否则其显示不会自动更新。

\n