我有两个文件,一个显然比另一个更新,但是在排序时max函数os.path.getctime不返回最新的文件。在
$ ls -lpat /foo.tar.gz
-rw-r--r-- 1 appsc appsc 29653389 May 21 15:05 /foo.tar.gz
$ ls -lpat /bar.tar.gz
-rw-r--r-- 1 appsc appsc 29653554 May 27 17:30 /bar.tar.gz
$ date
Wed Jun 4 01:23:29 UTC 2014
$ python3
Python 3.3.2 (default, Nov 6 2013, 12:16:42)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> compare = ['/bar.tar.gz', '/foo.tar.gz']
>>> print(max(compare, key=os.path.getctime))
/foo.tar.gz
>>> print(max(compare, key=lambda x: os.path.getctime(x)))
/foo.tar.gz
>>>
一定是我错过了什么。。。在
谢谢你的帮助
我相信ls-t显示的是最后一个时间,而不是最后一个ctime,这就是您使用的键。虽然mtime和ctime是相似的,但它们并不完全相同参见Difference between python - getmtime() and getctime() in unix system。在
相关问题 更多 >
编程相关推荐