我试图获得在特定时间创建的特定文件夹中的目录列表(例如,在第11小时创建的所有文件)。下面是我的代码:
import time
import os
def get_information(directory):
file_dict={}
for i in os.listdir(directory):
a=os.stat(os.path.join(directory,i))
b=time.gmtime(a.st_ctime)
newTime=b.tm_hour
if(file_dict.has_key(newTime)):
file_dict[newTime].append(i)
else:
file_dict[newTime]=[]
file_dict[newTime].append(i)
return file_dict
虽然我可以看到在第11个小时内创建的文件,但上面的代码无法提取它。
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181071677
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181071621
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181071661
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181071625
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181071673
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181055504
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181055500
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181055508
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181055512
drwxrwxr-x 2 oamops oamgroup 512 Aug 21 11:23 69-2181055516
另一个需要注意的是,从第0小时到第5小时有数据,然后从第18小时开始有数据
尝试创建一个过滤器函数:
import os, datetime
def check_time(path):
t = datetime.datetime.fromtimestamp(os.path.getmtime(path))
return t.hour == 11
然后过滤目录:
filtered = filter(check_time, os.listdir(r'my/path'))