遍历获取指定时间区间修改过的文件
python
import os
import time
start_time = '2018-09-11 00:00:00'
start_time_arr = time.strptime(start_time, "%Y-%m-%d %H:%M:%S")
start_time_ = time.mktime(start_time_arr)
# print(start_time_)
end_time = '2018-09-11 23:59:59'
end_time_arr = time.strptime(end_time, "%Y-%m-%d %H:%M:%S")
end_time_ = time.mktime(end_time_arr)
# print(end_time_)
def search_f(f):
fs = os.listdir(f)
for f1 in fs:
tmp_path = os.path.join(f, f1)
if not os.path.isdir(tmp_path):
# print(os.stat(tmp_path).st_mtime)
if os.stat(tmp_path).st_mtime >= start_time_:
if os.stat(tmp_path).st_mtime <= end_time_:
time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.stat(tmp_path).st_mtime))
print('文件: %s' % os.path.basename(tmp_path), time1)
with open(tmp_path, 'r') as rf:
print(rf.readline())
else:
search_f(tmp_path)
if __name__ == '__main__':
print(os.getcwd() + '/dist')
search_f(os.getcwd() + '/dist')
import os
import time
start_time = '2018-09-11 00:00:00'
start_time_arr = time.strptime(start_time, "%Y-%m-%d %H:%M:%S")
start_time_ = time.mktime(start_time_arr)
# print(start_time_)
end_time = '2018-09-11 23:59:59'
end_time_arr = time.strptime(end_time, "%Y-%m-%d %H:%M:%S")
end_time_ = time.mktime(end_time_arr)
# print(end_time_)
def search_f(f):
fs = os.listdir(f)
for f1 in fs:
tmp_path = os.path.join(f, f1)
if not os.path.isdir(tmp_path):
# print(os.stat(tmp_path).st_mtime)
if os.stat(tmp_path).st_mtime >= start_time_:
if os.stat(tmp_path).st_mtime <= end_time_:
time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.stat(tmp_path).st_mtime))
print('文件: %s' % os.path.basename(tmp_path), time1)
with open(tmp_path, 'r') as rf:
print(rf.readline())
else:
search_f(tmp_path)
if __name__ == '__main__':
print(os.getcwd() + '/dist')
search_f(os.getcwd() + '/dist')