Refs #22080 - Use NamedTemporaryFile instead of mkstemp

- Use *with* statement with temporary files in order to ensure that
  they are safely closed before exit