finalize() is called even if the object is not completely constructed.  self.metadata_file_handle may not exist if __init__ raises an exception first or not called at all.  Probably should try:except for (at least) AttributeError here.