-
Notifications
You must be signed in to change notification settings - Fork 21
/
list_files.py
30 lines (27 loc) · 1.17 KB
/
list_files.py
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
import os
import fnmatch
# does not operate recursively
def list_directories(directory, exclude_prefixes=('.',)):
for f in os.listdir(directory):
if f.startswith(exclude_prefixes):
continue
joined = os.path.join(directory, f)
if os.path.isdir(joined):
yield joined
# extensions can be a string or list, can include the preceding . or not
# operates recursively
def list_files(directory, extensions=None, exclude_prefixes=('.',)):
if type(extensions) == str:
extensions = [extensions]
if extensions is not None:
extensions = [('' if e.startswith('.') else '.') + e for e in extensions]
for root, dirnames, filenames in os.walk(directory):
filenames = [f for f in filenames if not f.startswith(exclude_prefixes)]
dirnames[:] = [d for d in dirnames if not d.startswith(exclude_prefixes)]
for filename in filenames:
base, ext = os.path.splitext(filename)
joined = os.path.join(root, filename)
if extensions is None or ext.lower() in extensions:
yield joined
def get_stem(fn):
return os.path.splitext(os.path.basename(fn))[0]