vinson
vinson

Reputation: 15

How to exclude directory in os.walk()?

I want to search my computer drives D to Z for all vhdx files, and calculate the total amount of them. But I want to exclude directories. How to change my code?

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)

Upvotes: 1

Views: 1037

Answers (2)

hiro protagonist
hiro protagonist

Reputation: 46849

this is how i usually exclude directories when iterating over os.walk:

for root, dirs, files in os.walk(drv):
    dirs[:] = [d for d in dirs if d not in extf]

the point here is to use a slice-assignment (dirs[:] = ...) in order to change dirs in-place (reassigning dirs to the newly created list).

if you want to have a slight speedup, i suggest to turn extf into a set:

extf = set(('$RECYCLE.BIN','System Volume Information'))

Upvotes: 1

Waket Zheng
Waket Zheng

Reputation: 6331

An example for you:

from pathlib import Path
i = 0
az = lambda: (chr(i)+":\\" for i in range(ord("D"), ord("Z") + 1))

for d in az():
    p = Path(d)
    if not p.exists():
        continue
    i += len(list(p.rglob('*.vhdx')))
print("total vhdx files:", i)

Upvotes: 0

Related Questions