Python – Use glob or os.walk() to ignore files in specific directories

Use glob or os.walk() to ignore files in specific directories… here is a solution to the problem.

Use glob or os.walk() to ignore files in specific directories

I

want to exclude directory ‘dir3_txt’ so that I can only capture files('.txt') from other directories. I’m trying to exclude directories like this, but can’t figure out how to get all files with .txt as ext and other files that have it in dir3_txt

:

for root, dirs, files in os.walk('.'):
   print (root)
   dirs[:] = [d for d in dirs if not d.startswith('dir3')]
   for file in files:
        print (os.path.join(root, file))

I’m thinking about glob (taken from the stack itself) but not sure how to tweak glob to use it.

for file in os.walk('.'):
   for txt in glob(os.path.join(files[0], '*.txt')):
       print(txt)

I experienced Excluding directories in os.walk But the solution provided didn’t help me, and it just told me to skip directories doesn’t help either, because I need to get files from other directories, it would be better if we could only use glob?

Solution

A simple solution is to perform a string comparison of the directory path and file returned by os.walk:

for root, dirs, files in os.walk('.'):
   if "/dir3_txt/" not in root:
       for file in files:
            if file.endswith(".txt"):
                print (os.path.join(root, file))

Related Problems and Solutions