问题
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...
Thanks for the help
回答1:
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.
来源:https://stackoverflow.com/questions/24027633/python-os-path-getctime-max-does-not-return-latest