Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate double asterisk (star) in path?

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?

like image 893
Anirban Nag 'tintinmj' Avatar asked Sep 19 '25 07:09

Anirban Nag 'tintinmj'


1 Answers

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))
like image 106
Paritosh Singh Avatar answered Sep 21 '25 06:09

Paritosh Singh