aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/utils/filewalker.py
blob: 3c36926bb913a3cdaf67abab382961a38c6bd616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# standard imports
import os
import typing

# exports
__all__: typing.Sequence[str] = (
    'list_files',
    )


## code ##

def list_files(
        roots: typing.Iterable[str],
        recursive: bool = True,
        follow_symlinks: bool = True,
        ) -> typing.Iterator[str]:
    """Iterate over all files in *roots*, recursively by default."""
    # index input paths
    for path in roots:
        if not os.path.exists(path):
            continue
        elif os.path.isdir(path) and recursive:
            for dirpath, _, filenames in os.walk(path, topdown=True, followlinks=follow_symlinks):
                for filename in filenames:
                    yield os.path.join(dirpath, filename)
        elif os.path.isfile(path):
            yield path

## EOF ##