-
Notifications
You must be signed in to change notification settings - Fork 2
/
getfiles.py
144 lines (115 loc) · 4.33 KB
/
getfiles.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
The :mod:`pyfan.util.path.getfiles` generate and get various file paths.
import pyfan.util.path.getfiles as getfiles
Includes method :func:`gen_path_file`, :func:`gen_path`
"""
import os as os
from pathlib import Path
import pyfan.util.inout.iosupport as pyfan_iosup
def gen_path_file(spt_folder='', snm_file='', st_file_type=''):
"""Return full path to file given folder path and file name with suffix
Parameters
----------
spt_folder : str
full path to folder
snm_file : str
file name without suffix (or with)
st_file_type : str
type of file, see options below, with pre-determined suffix, by default, there is no suffix. so if specify
nothing, will save the `snm_file` with what is externally fed in
Returns
-------
str
Full path to file with path and file name.
Examples
--------
>>> gen_path_file(spt_folder = 'C:/Users/fan/logvig/parameters/combo_type/',
... snm_file = 'fs_gen_combo_type_20201030', st_file_type='log')
C:\\Users\\fan\\logvig\\parameters\\combo_type\\fs_gen_combo_type_20201030.log.py
"""
st_suffix = ''
if st_file_type == 'log':
st_suffix = '.log.py'
return os.path.join(spt_folder, snm_file + st_suffix)
def gen_path(spt_root='', main_folder_name='', sub_folder_name=None, subsub_folder_name=None):
"""Generate a file path and return it
Parameters
----------
spt_root : str
root name
main_folder_name : str
folder name
sub_folder_name : str, optional
subfolder name
subsub_folder_name : str
subsub folder name
Returns
-------
str
the full path to the folder
Examples
--------
>>> gen_path(spt_root = 'C:/Users/fan/',
... main_folder_name='logvig', sub_folder_name='parameters',
... subsub_folder_name='combo_type')
C:\\Users\\fan\\logvig\\parameters\\combo_type\\
"""
spn_path_full = os.path.join(spt_root, main_folder_name, '')
if sub_folder_name is not None:
spn_path_full = pyfan_iosup.csvIO().createDirectory(spn_path_full, sub_folder_name)
if subsub_folder_name is not None:
spn_path_full = pyfan_iosup.csvIO().createDirectory(spn_path_full, subsub_folder_name)
return spn_path_full
def fp_search_rglob(spt_root="../",
st_rglob='*.py',
ls_srt_subfolders=None,
verbose=False,
):
"""Searches for files with search string in a list of folders
Parameters
----------
spt_root : string
root folder to search in
st_rglob : string
search for files with this rglob string
ls_srt_subfolders : :obj:`list` of :obj:`str`
a list of subfolders of the spt_root to search in
verbose: bool
print details
Returns
-------
:obj:`list` of :obj:`str`
A list of file names
Examples
--------
>>> ls_spn = fp_search_rglob(spt_root="../",
>>> ls_srt_subfolders=['rmd', 'pdf'],
>>> st_rglob = '*d*.py')
[WindowsPath('../rmd/bookdownparse.py'), WindowsPath('../rmd/mattexmd.py'), WindowsPath('../rmd/rmdparse.py'), WindowsPath('../pdf/pdfgen.py')]
"""
# Directories to search in
if ls_srt_subfolders is None:
# ls_srt_subfolders = ['calconevar/', 'derivative/', 'derivative_application/',
# 'matrix_basics/',
# 'opti_firm_constrained/', 'opti_hh_constrained_brsv/',
# 'opti_hh_constrained_brsv_inequality/']
# ls_srt_subfolders = ['matrix_basics/']
ls_spt = [spt_root]
else:
ls_spt = [spt_root + srt_subf for srt_subf in ls_srt_subfolders]
if verbose:
print(ls_spt)
# get file names
ls_spn_found_tex = [spn_file for spt_srh in ls_spt
for spn_file in Path(spt_srh).rglob(st_rglob)]
if verbose:
print(ls_spn_found_tex)
return ls_spn_found_tex
# if __name__ == '__main__':
# spt_root_u = "../"
# ls_srt_subfolders_u = ['rmd', 'pdf']
# st_rglob_u = '*d*.py'
# verbose_u = False
# ls_spn = fp_search_rglob(spt_root=spt_root_u, ls_srt_subfolders=ls_srt_subfolders_u,
# st_rglob=st_rglob_u, verbose=verbose_u)
# print(ls_spn)