exclude two directories in glob.glob()

Viewed 261

I have the following list of directories and sub-directories.

import glob
zipfiles = 'src/**/**/data.nq.gz'
filelist = glob.glob(zipfiles,recursive = True)

From this list, I need to exclude two directories namely 'src/27/' and 'src/c1/'

How do I do that?

P.S. src is a huge folder that contains tons of directories and subdirectories.

Thanks in advance

2 Answers

This should work:

filelist = [f for f in filelist if f[:7] not in ['src/27/', 'src/c1/']]

You could try

filelist = [file for file in filelist if not file.startswith("src/27/") and not file.startswith("src/c1/")]

Edit: Removed buggy solution that I first suggested.

Related