code: def close(self):
          
          # Check if any of the files have already been closed
          shp_open = self.shp and not (hasattr(self.shp, 'closed') and self.shp.closed)
          shx_open = self.shx and not (hasattr(self.shx, 'closed') and self.shx.closed)
          dbf_open = self.dbf and not (hasattr(self.dbf, 'closed') and self.dbf.closed)
              
          # Balance if already not balanced
          if self.shp and shp_open and self.dbf and dbf_open:
              if self.autoBalance:
                  self.balance()
              if self.recNum != self.shpNum:
                  raise ShapefileException("When saving both the dbf and shp file, "
                                           "the number of records (%s) must correspond "
                                           "with the number of shapes (%s)" % (self.recNum, self.shpNum))
          # Fill in the blank headers
          if self.shp and shp_open:
              self.__shapefileHeader(self.shp, headerType='shp')
          if self.shx and shx_open:
              self.__shapefileHeader(self.shx, headerType='shx')
  
          # Update the dbf header with final length etc
          if self.dbf and dbf_open:
              self.__dbfHeader()
  
          # Close files, if target is a filepath
          if self.target:
              for attribute in (self.shp, self.shx, self.dbf):
                  if hasattr(attribute, 'close'):
                      try:
                          attribute.close()
                      except IOError:
                          pass
docs: Write final shp, shx, and dbf headers, close opened files.