Getting previous file name

Miki miki.tebeka at gmail.com
Tue Aug 8 14:56:38 EDT 2006


Hello hj,

> I have a small script here that goes to inside dir and sorts the file
> by create date. I can return the create date but I don't know how to
> find the name of that file...
> I need file that is not latest but was created before the last file.
> Any hints... I am newbiw python dude and still trying to figure out lot
> of 'stuff'..
>
> ...
Remember that Python comes with "battaries included":
#!/usr/bin/env python

from os import walk
from os.path import getctime, join

def cmp_file_by_ctime(file1, file2):
    return cmp(getctime(file1), getctime(file2))

def walktree(path):
    file_list = []
    for root, dirs, files in walk(path):
        file_list += [join(root, file) for file in files]

    file_list.sort(cmp_file_by_ctime)
    return file_list[-2]

HTH,
Miki
http://pythonwise.blogspot.com/




More information about the Python-list mailing list