# Buggy code
def delete_unwanted_subtitles(dirpath, filename):
    """Deletes unwanted subtitles for the given filename in the specified dirpath

    :param dirpath: the directory path to be used
    :type dirpath: str
    :param filename: the subtitle filename
    :type dirpath: str
    """
    if (
        not sickbeard.SUBTITLES_MULTI
        or not sickbeard.SUBTITLES_KEEP_ONLY_WANTED
        or filename.rpartition(".")[2] not in subtitle_extensions
    ):
        return

    code = filename.rsplit(".", 2)[1].lower().replace("_", "-")
    language = from_code(code, unknown="") or from_ietf_code(code, unknown="und")

    if language.opensubtitles not in sickbeard.SUBTITLES_LANGUAGES:
        try:
            os.remove(os.path.join(dirpath, filename))
            logger.debug(
                "Deleted '%s' because we don't want subtitle language '%s'. We only want '%s' language(s)",
                filename,
                language,
                ",".join(sickbeard.SUBTITLES_LANGUAGES),
            )
        except Exception as error:
            logger.info("Couldn't delete subtitle: %s. Error: %s", filename, ex(error))


# Traceback (UnicodeEncodeError)
FINDSUBTITLES :: [d47493d] Traceback (most recent call last):
File "/home/home/SickRage/sickbeard/subtitles.py", line 338, in download_best_subs
save_subtitles(video, found_subtitles, directory=subtitles_dir, single=not sickbeard.SUBTITLES_MULTI)
File "/home/home/SickRage/lib/subliminal/core.py", line 695, in save_subtitles
with io.open(subtitle_path, 'wb') as f:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 93: ordinal not in range(128)
Traceback (most recent call last):
File "/home/home/SickRage/sickbeard/subtitles.py", line 338, in download_best_subs
save_subtitles(video, found_subtitles, directory=subtitles_dir, single=not sickbeard.SUBTITLES_MULTI)
File "/home/home/SickRage/lib/subliminal/core.py", line 695, in save_subtitles
with io.open(subtitle_path, 'wb') as f:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 93: ordinal not in range(128)

# Fixed code
def delete_unwanted_subtitles(dirpath, filename):
    """Delete unwanted subtitles for the given filename in the specified dirpath.

    :param dirpath: the directory path to be used
    :type dirpath: str
    :param filename: the subtitle filename
    :type dirpath: str
    """
    if (
        not sickbeard.SUBTITLES_MULTI
        or not sickbeard.SUBTITLES_KEEP_ONLY_WANTED
        or filename.rpartition(".")[2] not in subtitle_extensions
    ):
        return

    code = filename.rsplit(".", 2)[1].lower().replace("_", "-")
    language = from_code(code, unknown="") or from_ietf_code(code, unknown="und")

    if language.opensubtitles not in sickbeard.SUBTITLES_LANGUAGES:
        try:
            os.remove(os.path.join(dirpath, filename))
            logger.debug(
                "Deleted '%s' because we don't want subtitle language '%s'. We only want '%s' language(s)",
                filename,
                language,
                ",".join(sickbeard.SUBTITLES_LANGUAGES),
            )
        except Exception as error:
            logger.info("Couldn't delete subtitle: %s. Error: %s", filename, ex(error))