Chinaunix首页 | 论坛 | 博客
  • 博客访问: 191848
  • 博文数量: 79
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 15
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-25 14:40
文章分类
文章存档

2015年(1)

2014年(1)

2013年(2)

2012年(1)

2011年(5)

2010年(2)

2009年(6)

2008年(20)

2007年(27)

2006年(14)

我的朋友

分类: Python/Ruby

2007-09-02 15:57:29

1.
      # retrieve the file information from a selected folder
   2.
      # sort the files by last modified date/time and display in order newest file first
   3.
      # tested with Python24 vegaseat 21jan2006
   4.
       
   5.
      import os, glob, time
   6.
       
   7.
      # use a folder you have ...
   8.
      root = 'D:\\Zz1\\Cartoons\\' # one specific folder
   9.
      #root = 'D:\\Zz1\\*' # all the subfolders too
  10.
       
  11.
      print '-'*60 # just vanity
  12.
       
  13.
      date_file_list = []
  14.
      for folder in glob.glob(root):
  15.
      print "folder =", folder
  16.
      # select the type of file, for instance *.jpg or all files *.*
  17.
      for file in glob.glob(folder + '/*.*'):
  18.
      # retrieves the stats for the current file as a tuple
  19.
      # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
  20.
      # the tuple element mtime at index 8 is the last-modified-date
  21.
      stats = os.stat(file)
  22.
      # create tuple (year yyyy, month(1-12), day(1-31), hour(0-23), minute(0-59), second(0-59),
  23.
      # weekday(0-6, 0 is monday), Julian day(1-366), daylight flag(-1,0 or 1)) from seconds since epoch
  24.
      # note: this tuple can be sorted properly by date and time
  25.
      lastmod_date = time.localtime(stats[8])
  26.
      #print image_file, lastmod_date # test
  27.
      # create list of tuples ready for sorting by date
  28.
      date_file_tuple = lastmod_date, file
  29.
      date_file_list.append(date_file_tuple)
  30.
  31.
      #print date_file_list # test
  32.
       
  33.
      date_file_list.sort()
  34.
      date_file_list.reverse() # newest mod date now first
  35.
       
  36.
      print "%-40s %s" % ("filename:", "last modified:")
  37.
      for file in date_file_list:
  38.
      # extract just the filename
  39.
      folder, file_name = os.path.split(file[1])
  40.
      # convert date tuple to MM/DD/YYYY HH:MM:SS format
  41.
      file_date = time.strftime("%m/%d/%y %H:%M:%S", file[0])
  42.
      print "%-40s %s" % (file_name, file_date)

阅读(1745) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~