RonyA
RonyA

Reputation: 625

Exclude specific folders and subfolders in os.walk

List all the files having ext .txt in the current directory .

L = [txt for f in os.walk('.') 
            for txt in glob(os.path.join(file[0], '*.txt'))]

I want to avoid files from one specific directory and its subdirectories . Lets say I do not want to dig into folder3 and its available subdirectories to get the .txt files. I tried below

d = list(filter(lambda x : x != 'folder3', next(os.walk('.'))[1]))

but further steps not able to figure it out.How to include both to work together?

EDIT:

I tried referring the link provided as already answered query but I am unable to get desired output with below and surprisingly getting empty list as output for a

a=[]
for root, dirs, files in os.walk('.'):
  dirs[:] = list(filter(lambda x : x != 'folder3', dirs)) 
  for txt in glob(os.path.join(file[0], '*.txt')): 
      a.append(txt)

Upvotes: 2

Views: 3473

Answers (1)

Isma
Isma

Reputation: 15190

The following solution seems to be working, any directory specified in the exclude set will be ignored, any extension in the extensions set will be included.

import os

exclude = set(['folder3'])
extensions = set(['.txt', '.dat'])
for root, dirs, files in os.walk('c:/temp/folder', topdown=True):
    dirs[:] = [d for d in dirs if d not in exclude]
    files = [file for file in files if os.path.splitext(file)[1] in extensions]
    for fname in files:
        print(fname)

This code uses the option topdown=True to modify the list of dir names in place as specified in the docs:

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search

Upvotes: 7

Related Questions