[Tutor] Finding the latest file in a dir

Joseph John jjk.saji at gmail.com
Sun Jan 5 13:38:07 CET 2014


Hi All,
Even though I am a old user in the mailing list, my skills in Python are
not impressive.
I have restarted doing python for some small task.
I am trying to find the latest file in a dir, I have searched and found out
I shoulld do in this way
----------------
#!/usr/bin/python

import os
newest = max(os.listdir("/opt/AlfDB/."), key = os.path.getctime)
print newest
-----------------------
but when I run it I am getting error message such as
./FindingLatestFileInDir.py
Traceback (most recent call last):
  File "./FindingLatestFileInDir.py", line 4, in <module>
    newest = max(os.listdir("/opt/AlfDB/."), key = os.path.getctime)
  File "/usr/lib64/python2.7/genericpath.py", line 64, in getctime
    return os.stat(filename).st_ctime
OSError: [Errno 2] No such file or directory: 'alfresco20140104-0000.sql'

------------------------------------------------------
I get results if I do it without giving the dir location such as
---
#!/usr/bin/python

import os
newest = max(os.listdir("."), key = os.path.getctime)
print newest
---------------------
This gives the result for the directory from where it runs, but when I
change the dir location to "/opt/AlfDB" , It does not. I am not able to
apprehend how it is not working

Guidance and advice requested
Thanks
Joseph John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140105/3cac4997/attachment.html>


More information about the Tutor mailing list