2

Any double asterisk in a path means all sub-directories.

Now if I have a path like this a/b/c/**/*.txt Which means I need all files under all sub-directories under c. How to get that in python?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59

1 Answers1

5

Let's consider an example. Consider a directory containing the following files: 1.gif, 2.txt, card.gif and a subdirectory sub which contains only the file 3.txt.

Update:

pathlib now offers an amazing interface for most common path operations. This task can also be done via pathlib as follows:

from pathlib import Path

path = Path(r".") # path to the root dir from where you want to start searching

list(path.glob("**/*.txt"))
Out[1]: [WindowsPath('2.txt'), WindowsPath('sub/3.txt')]

For older answers see below


Using glob.

From Docs:

glob() will produce the following results. Notice how any leading components of the path are preserved.

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True) #python 3.5+
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']

For python < 3.5

For older Python versions, use os.walk to recursively walk a directory and fnmatch.filter to match against a simple expression:

import fnmatch
import os

matches = []
for root, dirnames, filenames in os.walk('src'):
    for filename in fnmatch.filter(filenames, '*.txt'):
        matches.append(os.path.join(root, filename))
Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33