[Tutor] Finding the latest file in a dir

Dominik George nik at naturalnet.de
Sun Jan 5 13:45:25 CET 2014


Hi,

> 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

That is because the getctime call will run based in the current working
directory. You can use a construct like:

  max([os.path.join('opt', 'foo', x) for x in os.listdir(os.path.join('opt', 'foo')], key=os.path.getctime)

That uses a list comprehension to prepend the full path to the results.
You could also chdir() to the location beforehand.

Please always use os.path.join().

-nik

-- 
<Natureshadow> Auf welchem Server liegt das denn jetzt…?
<mirabilos> Wenn es nicht übers Netz kommt bei Hetzner, wenn es nicht
            gelesen wird bei STRATO, wenn es klappt bei manitu.

PGP-Fingerprint: 3C9D 54A4 7575 C026 FB17  FD26 B79A 3C16 A0C4 F296
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 905 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/tutor/attachments/20140105/495cb05a/attachment.sig>


More information about the Tutor mailing list