这@Greg Hewgill是答案的更详细的版本。这是最符合问题要求的。它区分了创建日期和修改日期(至少在Windows上如此)。
#!/usr/bin/env pythonfrom stat import S_ISREG, ST_CTIME, ST_MODEimport os, sys, time# path to the directory (relative or absolute)dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.'# get all entries in the directory w/ statsentries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath))entries = ((os.stat(path), path) for path in entries)# leave only regular files, insert creation dateentries = ((stat[ST_CTIME], path)for stat, path in entries if S_ISREG(stat[ST_MODE]))#NOTE: on Windows `ST_CTIME` is a creation date # but on Unix it could be something else#NOTE: use `ST_MTIME` to sort by a modification datefor cdate, path in sorted(entries): print time.ctime(cdate), os.path.basename(path)
例:
$ python stat_creation_date.pyThu Feb 11 13:31:07 2009 stat_creation_date.py
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)