forked from jevalenciap/training
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_search.py
More file actions
59 lines (42 loc) · 1.63 KB
/
file_search.py
File metadata and controls
59 lines (42 loc) · 1.63 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
"""Recursive Directory Search
This module provides functionalities that facilitate the recursive search of
files and folders based on a specific extention.
"""
import os
import fnmatch
def find_sources(ext, mode, directory='challenges/'):
"""Fn to find sources based on an ext under the default dir, challenges.
Args:
ext (string): Extention of desired files.
mode (int) : 0 for any file name with desired ext. 1 for OTHERS.
directory (string): Default dir 'challenges/'.
Returns:
List: matches[]. Contains list of file names with desired ext.
"""
matches = []
# iterate through dirs and append to matches
for root, _, filenames in os.walk(directory):
if mode == 0:
for filename in fnmatch.filter(filenames, '*.' + ext):
matches.append(os.path.join(root, filename))
else:
for filename in fnmatch.filter(filenames, 'OTHERS.' + ext):
matches.append(os.path.join(root, filename))
return matches
def find_folders(directory='challenges/'):
"""Fn to find folder names under default dir, challenges.
Args:
directory (string): Default dir 'challenges/'.
Returns:
List: matches[]. Contains list of folder names.
"""
matches = []
# iterate through dirs and append to matches
for root, dirs, filenames in os.walk(directory):
if filenames and not dirs:
fname_matches = []
for name in filenames:
fname_matches.append(os.path.join(root, name))
matches.append(fname_matches)
return matches