python按日期排序_你如何在python中获得按创建日期排序的目录列表?
jfs..
55
這是一個(gè)更詳細(xì)@Greg Hewgill的答案.它最符合問(wèn)題要求.它區(qū)分了創(chuàng)建和修改日期(至少在Windows上).
#!/usr/bin/env python
from stat import S_ISREG, ST_CTIME, ST_MODE
import 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/ stats
entries = (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 date
entries = ((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 date
for cdate, path in sorted(entries):
print time.ctime(cdate), os.path.basename(path)
例:
$ python stat_creation_date.py
Thu Feb 11 13:31:07 2009 stat_creation_date.py
這可行,但最簡(jiǎn)潔的解決方案是http://stackoverflow.com/a/4500607/68534 (4認(rèn)同)
總結(jié)
以上是生活随笔為你收集整理的python按日期排序_你如何在python中获得按创建日期排序的目录列表?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 矫正拔掉的牙齿多久能收缝
- 下一篇: 大牙嵌体修复多少钱