# Buggy 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 app.SUBTITLES_MULTI
        or not app.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)

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


# Traceback (LanguageConvertError)
2017-12-09 23:55:41 ERROR    FINDSUBTITLES :: [1a8dc66] Exception generated: srt
Traceback (most recent call last):
File "/opt/medusa/<a href="../blob/1a8dc66c7ec9231be0d3444710e476437c9ae1c8/medusa/scheduler.py#L90">medusa/scheduler.py</a>", line 90, in run
self.action.run(self.force)
File "/opt/medusa/<a href="../blob/1a8dc66c7ec9231be0d3444710e476437c9ae1c8/medusa/subtitles.py#L948">medusa/subtitles.py</a>", line 948, in run
self.subtitles_download_in_pp()
File "/opt/medusa/<a href="../blob/1a8dc66c7ec9231be0d3444710e476437c9ae1c8/medusa/subtitles.py#L842">medusa/subtitles.py</a>", line 842, in subtitles_download_in_pp
delete_unwanted_subtitles(root, filename)
File "/opt/medusa/<a href="../blob/1a8dc66c7ec9231be0d3444710e476437c9ae1c8/medusa/subtitles.py#L796">medusa/subtitles.py</a>", line 796, in delete_unwanted_subtitles
if language.opensubtitles not in app.SUBTITLES_LANGUAGES:
File "/opt/medusa/<a href="../blob/1a8dc66c7ec9231be0d3444710e476437c9ae1c8/ext/babelfish/language.py#L150">ext/babelfish/language.py</a>", line 150, in __getattr__
return language_converters[name].convert(alpha3, country, script)
File "/opt/medusa/<a href="../blob/1a8dc66c7ec9231be0d3444710e476437c9ae1c8/ext/babelfish/converters/opensubtitles.py#L23">ext/babelfish/converters/opensubtitles.py</a>", line 23, in convert
alpha3b = self.alpha3b_converter.convert(alpha3, country, script)
File "/opt/medusa/<a href="../blob/1a8dc66c7ec9231be0d3444710e476437c9ae1c8/ext/babelfish/converters/__init__.py#L159">ext/babelfish/converters/__init__.py</a>", line 159, in convert
raise LanguageConvertError(alpha3, country, script)
LanguageConvertError: srt

# 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 app.SUBTITLES_MULTI
        or not app.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)
    found_language = None
    try:
        found_language = language.opensubtitles
    except LanguageConvertError:
        logger.info("Unable to convert language code '%s' for: %s", code, filename)

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