python os.listdir按文件存取时间顺序列出目录的实例

如下所示:

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

相关推荐