python按时间排序目录下的文件

按时间升序

import os

DIR = "/home/serho/workspace/lisp"

def compare(x, y):
    stat_x = os.stat(DIR + "/" + x)
    stat_y = os.stat(DIR + "/" + y)
    if stat_x.st_ctime < stat_y.st_ctime:
        return -1
    elif stat_x.st_ctime > stat_y.st_ctime:
        return 1
    else:
        return 0

iterms = os.listdir(DIR)

iterms.sort(compare)

for iterm in iterms:
    print iterm
按时间降序

import os

DIR = "/home/serho/workspace/lisp"

def compare(x, y):
    stat_x = os.stat(DIR + "/" + x)
    stat_y = os.stat(DIR + "/" + y)
    if stat_x.st_ctime > stat_y.st_ctime:
        return -1
    elif stat_x.st_ctime < stat_y.st_ctime:
        return 1
    else:
        return 0

iterms = os.listdir(DIR)

iterms.sort(compare)

for iterm in iterms:
    print iterm
来源: http://python-china.org/topic/185

你可能感兴趣的:(PYTHON,Django)