'Write the item\'s metadata to a media file.
All fields in `_media_fields` are written to disk according to
the values on this object.
`path` is the path of the mediafile to write the data to. It
defaults to the item\'s path.
`tags` is a dictionary of additional metadata the should be
written to the file. (These tags need not be in `_media_fields`.)
Can raise either a `ReadError` or a `WriteError`.'

def write(self, path=None, tags=None):

    if (path is None):
        path = self.path
    else:
        path = normpath(path)
    item_tags = dict(self)
    item_tags = {k: v for (k, v) in item_tags.items() if (k in self._media_fields)}
    if (tags is not None):
        item_tags.update(tags)
    plugins.send('write', item=self, path=path, tags=item_tags)
    try:
        mediafile = MediaFile(syspath(path), id3v23=beets.config['id3v23'].get(bool))
    except UnreadableFileError as exc:
        raise ReadError(path, exc)
    mediafile.update(item_tags)
    try:
        mediafile.save()
    except UnreadableFileError as exc:
        raise WriteError(self.path, exc)
    if (path == self.path):
        self.mtime = self.current_mtime()
    plugins.send('after_write', item=self, path=path)
