@@ -178,12 +178,14 @@ def search(self, query: str, result_format: str = "xml") -> list[Any]:
178178 return recs
179179
180180 def write_search_results_to_file (
181- self , query : str = "" , output_file_name : str = "tind.xml"
181+ self , query : str = "" , output_file_name : str = "tind.xml" , output_dir : str = ""
182182 ) -> int :
183183 """Search TIND and stream results to an XML file.
184184
185185 :param str query: A TIND search query string.
186186 :param str output_file_name: filename for the output XML file.
187+ :param str output_dir: Directory in which to save the file.
188+ Falls back to ``default_storage_dir`` when empty.
187189 :returns int: The number of records written to the file.
188190 """
189191
@@ -192,9 +194,9 @@ def write_search_results_to_file(
192194 return 0
193195
194196 recs_written = 0
195- output_path = os . path . join ( self .default_storage_dir , output_file_name )
197+ output_path = Path ( output_dir or self .default_storage_dir ) / output_file_name
196198 try :
197- with open (output_path , "w" , encoding = "utf-8" ) as f :
199+ with output_path . open ("w" , encoding = "utf-8" ) as f :
198200 f .write (f'<?xml version="1.0" encoding="UTF-8"?>\n <collection xmlns="{ NS } ">\n ' )
199201 for record in self ._iter_xml_records (query ):
200202 record_xml = E .tostring (record , encoding = "unicode" )
@@ -206,7 +208,7 @@ def write_search_results_to_file(
206208 raise TINDError (f"Matched { total_hits } tind ids, but API did not return any." )
207209 f .write ("</collection>\n " )
208210 except Exception :
209- Path ( output_path ) .unlink (missing_ok = True )
211+ output_path .unlink (missing_ok = True )
210212 raise
211213
212214 if recs_written != total_hits :
0 commit comments