我要在我的计算机驱动器D到Z中搜索所有vhdx文件,并计算它们的总量。但我想排除目录。如何更改我的代码?你知道吗
extf = ['$RECYCLE.BIN','System Volume Information']
import os
i = 0
az = lambda: (chr(i)+":\\" for i in range(ord("D"), ord("Z") + 1))
for drv in az():
for root, dirs, files in os.walk(drv):
for filename in files:
splitname = filename.split('.')
if splitname[-1] !="vhdx":
continue
file_path = (os.path.join(root, filename))
print file_path
i += 1
if i != 0:
print ("total vhdx files:",i)
举个例子:
这就是我在迭代^{} 时通常排除目录的方式:
这里的要点是使用slice-assignment(
dirs[:] = ...
)来更改dirs
(将dirs
重新分配给新创建的列表)。你知道吗如果你想稍微加速,我建议把
extf
变成set
:相关问题 更多 >
编程相关推荐