python os.path.getctime max不返回最新 [英] python os.path.getctime max does not return latest

查看:29
本文介绍了python os.path.getctime max不返回最新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,一个显然要比另一个新,但是按os.path.getctime排序时的max函数不会返回最新文件.

I have two files, one clearly more recent then the other, however max function when sorting by os.path.getctime does not return the most recent file.

$ 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
>>> 

那里一定有我想念的东西...

There must be something I am missing there...

感谢您的帮助

推荐答案

我相信ls -t显示的是最后一个mtime而不是最后一个ctime,这就是您要用作键的时间.尽管mtime和ctime相似,但它们并不完全相同-请参阅

I believe that ls -t displays the last mtime rather than the last ctime, which is what you're using as the key. Though mtime and ctime are similar, they are not quite the same -- see Difference between python - getmtime() and getctime() in unix system for reference.

这篇关于python os.path.getctime max不返回最新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆