[ci] Correct file management in tests #18341
Refactor the helper function to close the file immediately after writing
 > On Windows, attempting to remove a file that is in use causes an
This resolves gh-18255.
The `mkstemp` function creates a file and opens that file , so these tests erroneously attempted to delete an open file. This mistake was not identified during development or automated testing because it only leads to a runtime exception in Windows environments . Refactor the helper function to close the file immediately after writing to it.  > `mkstemp()` returns a tuple containing an OS-level handle to an > open file (as would be returned by `os.open()`) and the absolute > pathname of that file, in that order. https://docs.python.org/2/library/tempfile.html#tempfile.mkstemp  > On Windows, attempting to remove a file that is in use causes an > exception to be raised; on Unix, the directory entry is removed > but the storage allocated to the file is not made available until > the original file is no longer in use. https://docs.python.org/2/library/os.html#os.remove