Skip to content

Commit 99be28b

Browse files
committed
Update to find_first_file() function so user can select whether exception is throw or none returned if a file is not found.
1 parent 254d07f commit 99be28b

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

pbprocesstools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
PB_PROCESS_TOOLS_VERSION_MAJOR = 1
4343
PB_PROCESS_TOOLS_VERSION_MINOR = 2
44-
PB_PROCESS_TOOLS_VERSION_PATCH = 3
44+
PB_PROCESS_TOOLS_VERSION_PATCH = 4
4545

4646
PB_PROCESS_TOOLS_VERSION = str(PB_PROCESS_TOOLS_VERSION_MAJOR) + "." + str(PB_PROCESS_TOOLS_VERSION_MINOR) + "." + str(PB_PROCESS_TOOLS_VERSION_PATCH)
4747
PB_PROCESS_TOOLS_VERSION_OBJ = LooseVersion(PB_PROCESS_TOOLS_VERSION)

pbprocesstools/pbpt_process.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,33 @@ def find_file(self, dir_path, file_search):
104104
return None
105105
return files[0]
106106

107-
def find_first_file(self, dirPath, fileSearch):
107+
def find_first_file(self, dirPath, fileSearch, rtn_except=True):
108108
"""
109109
Search for a single file with a path using glob. Therefore, the file
110110
path returned is a true path. Within the fileSearch provide the file
111111
name with '*' as wildcard(s).
112-
:param dirPath:
113-
:param fileSearch:
114-
:return:
112+
:param dirPath: The directory within which to search, note that the search will be within
113+
sub-directories within the base directory until a file meeting the search
114+
criteria are met.
115+
:param fileSearch: The file search string in the file name and must contain a wild character (i.e., *).
116+
:param rtn_except: if True then an exception will be raised if no file or multiple files are found (default).
117+
If False then None will be returned rather than an exception raised.
118+
:return: The file found (or None if rtn_except=False)
119+
115120
"""
116121
import glob
122+
files = None
117123
for root, dirs, files in os.walk(dirPath):
118124
files = glob.glob(os.path.join(root, fileSearch))
119125
if len(files) > 0:
120126
break
121-
122-
if len(files) != 1:
123-
raise Exception("Could not find a single file ({0}) in {1}; found {2} files.".format(fileSearch, dirPath, len(files)))
124-
return files[0]
127+
out_file = None
128+
if (files is not None) and (len(files) == 1):
129+
out_file = files[0]
130+
elif rtn_except:
131+
raise Exception("Could not find a single file ({0}) in {1}; "
132+
"found {2} files.".format(fileSearch, dirPath, len(files)))
133+
return out_file
125134

126135
def get_file_basename(self, filepath, checkvalid=False, n_comps=0):
127136
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import os
3838

3939
setuptools.setup(name='pb_process_tools',
40-
version='1.2.3',
40+
version='1.2.4',
4141
description='Tools for batch processing data, including on HPC cluster with slurm.',
4242
author='Pete Bunting',
4343
author_email='petebunting@mac.com',

0 commit comments

Comments
 (0)