forked from rranshous/findfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_files.py
More file actions
72 lines (56 loc) · 2.25 KB
/
Copy pathfind_files.py
File metadata and controls
72 lines (56 loc) · 2.25 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
60
61
62
63
64
65
66
67
68
69
70
71
import os
import glob
version_info = (0, 0, 2)
__version__ = '.'.join(map(str, version_info))
version = __version__
def find_dirs_iter(path,exclude=None):
""" yield back dir paths meeting extension/exclude criteria """
# absolute paths
found = []
for dir_path, dir_names, file_names in os.walk(path):
# remove paths which include the exclude string
# this will keep them from being traversed
bad_dirs = [x for x in dir_names if exclude in dir_names]
map(dir_names.remove,bad_dirs)
# see if any of the current files meet our
# extension and exclude criteria
for name in dir_names:
if (not exclude or exclude not in x):
yield os.path.abspath(os.path.join(dir_path,name))
def find_files_iter(path,extension=None,exclude=None,file_name=None):
""" yield back file paths meeting extension/exclude criteria """
# absolute paths
found = []
for dir_path, dir_names, file_names in os.walk(path):
# remove paths which include the exclude string
# this will keep them from being traversed
bad_dirs = [x for x in dir_names if exclude in dir_names]
map(dir_names.remove,bad_dirs)
# see if any of the current files meet our
# extension and exclude criteria
for name in file_names:
# if we specified a file name than we need to
# check for that match first
if file_name is not None:
if file_name != name:
continue
if (not extension or name.endswith(extension)) \
and (not exclude or exclude not in x):
yield os.path.abspath(os.path.join(dir_path,name))
def find_files(path,extension=None,exclude=None,file_name=None):
path = os.path.expanduser(path)
path_list = glob.glob(path)
res = []
for path in path_list:
r = [f for f in find_files_iter(path,extension,exclude,file_name)]
res.extend(r)
return res
def find_dirs(path,exclude=None):
path = os.path.expanduser(path)
path_list = glob.glob(path)
res = []
for path in path_list:
r = [f for f in find_dirs_iter(path,exclude)]
res.extend(r)
return res
# vim: ts=8 sts=4 expandtab shiftwidth=4