python,在两个日期之间搜索文件

时间:2015-07-21 20:45:42

标签: python

我想在python中创建脚本,在Linux中递归搜索两个日期之间的文件?

import os

for dirname, dirnames, filenames in os.walk('.'):

    for subdirname in dirnames:
        print(os.path.join(dirname, subdirname))

    # print path to all filenames.
    for filename in filenames:
        print(os.path.join(dirname, filename))

1 个答案:

答案 0 :(得分:1)

喜欢这个吗?

import os, datetime as dt

# define epoch time
t0 = dt.datetime.utcfromtimestamp(0)

# define time ranges
d1 = (dt.datetime(2015,1,1)  - t0).total_seconds()
d2 = (dt.datetime(2015,1,31) - t0).total_seconds()

for (dirpath, dirnames, filenames) in os.walk(path):
    for filename in filenames:
        f = '/'.join([dirpath,filename])
        ctime = os.stat(f)[-1]
        if ctime>=d1 and ctime <=d2: 
            print f