No idea. I assume this code works on not-macOS with an equal number of files?
COPY loads the file with Util.load_file, which raises an intended-to-be fatal exception if it can't close the file. The buffer is later written by
this code, which is printing the error and raising the exception in this case. The out file is also closed properly.
(Incidentally, both of the aforementioned exceptions are among those unsafe exceptions I was warning about a while back; using TRY to catch these exception and not terminating the installation would result in the mod continuing on, with the file remaining open.)
Edit: fixed code tag.