0

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

vinni_f
  • 622
  • 3
  • 15
  • 25

1 Answers1

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.

Community
  • 1
  • 1
jjwon
  • 204
  • 1
  • 8
  • Thanks, I'm ashamed of myself. How could I missed that.The worst is that all my code was done using getctime. It only behaved oddly for the first time yesterday after 2 weeks of production. – vinni_f Jun 04 '14 at 11:55