为清晰起见编辑:我试图获取特定文件夹和子文件夹中特定文件类型的url。我有一个文件夹列表,在散步期间我永远不想进入,名为bannedDir
。如果出现bannedDir
中的任何单词,我想从walk中完全跳过这些目录,我相信我已经做到了。我有一个名为flaggedDir
的正则表达式列表。如果flaggedDir
中的任何单词在根目录中,我想对该根目录下的所有内容做些什么。你知道吗
下面我要做的是,首先,排除在excludedDir
目录中的搜索,该目录包含来自bannedDir的所有条目和来自flaggedDir
的一些条目。然后我想从剩下的文件夹中获取所有xls文件或fileType
的mtime。然后存储我称之为iniList
的mtime列表中的最大mtime。你知道吗
下面是当前代码。你知道吗
for root, dirs, files in os.walk(topDir, topdown=True):
dirs[:] = [d for d in dirs if d not in bannedDir]
if flaggedDir.search(root) is not None:
dirs[:] = [d for d in dirs if d not in excludedDir]
for name in files:
if name.lower().endswith(fileTypes):
lastModif = [];
timeIndex = [];
fileLocation = os.path.join(root, name);
time = os.path.getmtime(fileLocation);
timeIndex.append(time);
lastModif.append([fileLocation,time]);
if len(lastModif) > 0:
iniList.append(max(lastModif, key=lambda item: item[1]));
比如说
topDir = [C:\\Test\]
fileTypes = '.xls'
bannedDir = [a,b]
flaggedDir = [c,d]
excludedDir = [a,b,c]
dir a -- file 1.xls,
dir b -- file 5.exe,
dir c -- file 2.exe,
dir d -- file 3.xls, file 4.exe, file 5.xls
我应该只能获取文件3.xls和文件5.xls,因为应该跳过目录a、b和c。然后我应该得到文件3.xls,因为3的mtime是5000,而文件5的mtime是2000。我的问题是,我似乎用我的代码在某些目录上遍历了两次。我也没有得到每个子目录的最大值。我该怎么解决这个问题?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐