@@ -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 """
0 commit comments