Skip to content

Commit

Permalink
Update to find_first_file() function so user can select whether excep…
Browse files Browse the repository at this point in the history
…tion is throw or none returned if a file is not found.
  • Loading branch information
petebunting committed Jul 5, 2020
1 parent 254d07f commit 99be28b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pbprocesstools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

PB_PROCESS_TOOLS_VERSION_MAJOR = 1
PB_PROCESS_TOOLS_VERSION_MINOR = 2
PB_PROCESS_TOOLS_VERSION_PATCH = 3
PB_PROCESS_TOOLS_VERSION_PATCH = 4

PB_PROCESS_TOOLS_VERSION = str(PB_PROCESS_TOOLS_VERSION_MAJOR) + "." + str(PB_PROCESS_TOOLS_VERSION_MINOR) + "." + str(PB_PROCESS_TOOLS_VERSION_PATCH)
PB_PROCESS_TOOLS_VERSION_OBJ = LooseVersion(PB_PROCESS_TOOLS_VERSION)
Expand Down
25 changes: 17 additions & 8 deletions pbprocesstools/pbpt_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,33 @@ def find_file(self, dir_path, file_search):
return None
return files[0]

def find_first_file(self, dirPath, fileSearch):
def find_first_file(self, dirPath, fileSearch, rtn_except=True):
"""
Search for a single file with a path using glob. Therefore, the file
path returned is a true path. Within the fileSearch provide the file
name with '*' as wildcard(s).
:param dirPath:
:param fileSearch:
:return:
:param dirPath: The directory within which to search, note that the search will be within
sub-directories within the base directory until a file meeting the search
criteria are met.
:param fileSearch: The file search string in the file name and must contain a wild character (i.e., *).
:param rtn_except: if True then an exception will be raised if no file or multiple files are found (default).
If False then None will be returned rather than an exception raised.
:return: The file found (or None if rtn_except=False)
"""
import glob
files = None
for root, dirs, files in os.walk(dirPath):
files = glob.glob(os.path.join(root, fileSearch))
if len(files) > 0:
break

if len(files) != 1:
raise Exception("Could not find a single file ({0}) in {1}; found {2} files.".format(fileSearch, dirPath, len(files)))
return files[0]
out_file = None
if (files is not None) and (len(files) == 1):
out_file = files[0]
elif rtn_except:
raise Exception("Could not find a single file ({0}) in {1}; "
"found {2} files.".format(fileSearch, dirPath, len(files)))
return out_file

def get_file_basename(self, filepath, checkvalid=False, n_comps=0):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import os

setuptools.setup(name='pb_process_tools',
version='1.2.3',
version='1.2.4',
description='Tools for batch processing data, including on HPC cluster with slurm.',
author='Pete Bunting',
author_email='[email protected]',
Expand Down

0 comments on commit 99be28b

Please sign in to comment.