# Buggy code
def find_key(self, items, write=False):
    overwrite = self.config["overwrite"].get(bool)
    command = [self.config["bin"].as_str()]
    # The KeyFinder GUI program needs the -f flag before the path.
    # keyfinder-cli is similar, but just wants the path with no flag.
    if "keyfinder-cli" not in os.path.basename(command[0]).lower():
        command.append("-f")

    for item in items:
        if item["initial_key"] and not overwrite:
            continue

        try:
            output = util.command_output(command + [util.syspath(item.path)]).stdout
        except (subprocess.CalledProcessError, OSError) as exc:
            self._log.error("execution failed: {0}", exc)
            continue
        except UnicodeEncodeError:
            # Workaround for Python 2 Windows bug.
            # https://bugs.python.org/issue1759845
            self._log.error("execution failed for Unicode path: {0!r}", item.path)
            continue

        key_raw = output.rsplit(None, 1)[-1]
        try:
            key = util.text_string(key_raw)
        except UnicodeDecodeError:
            self._log.error("output is invalid UTF-8")
            continue

        item["initial_key"] = key
        self._log.info(
            "added computed initial key {0} for {1}",
            key,
            util.displayable_path(item.path),
        )

        if write:
            item.try_write()
        item.store()


# Traceback (IndexError)
user configuration: /home/diomekes/.config/beets/config.yaml
data directory: /home/diomekes/.config/beets
plugin paths:
Sending event: pluginload
inline: adding item field disc_and_track
library database: /home/diomekes/.config/beets/library.db
library directory: /home/diomekes/media/music
Sending event: library_opened
Traceback (most recent call last):
File "/usr/bin/beet", line 9, in <module>
load_entry_point('beets==1.3.19', 'console_scripts', 'beet')()
File "/usr/lib/python2.7/site-packages/beets/ui/__init__.py", line 1266, in main
_raw_main(args)
File "/usr/lib/python2.7/site-packages/beets/ui/__init__.py", line 1253, in _raw_main
subcommand.func(lib, suboptions, subargs)
File "/usr/lib/python2.7/site-packages/beetsplug/keyfinder.py", line 48, in command
self.find_key(lib.items(ui.decargs(args)), write=ui.should_write())
File "/usr/lib/python2.7/site-packages/beetsplug/keyfinder.py", line 74, in find_key
key_raw = output.rsplit(None, 1)[-1]
IndexError: list index out of range

# Fixed code
def find_key(self, items, write=False):
    overwrite = self.config["overwrite"].get(bool)
    command = [self.config["bin"].as_str()]
    # The KeyFinder GUI program needs the -f flag before the path.
    # keyfinder-cli is similar, but just wants the path with no flag.
    if "keyfinder-cli" not in os.path.basename(command[0]).lower():
        command.append("-f")

    for item in items:
        if item["initial_key"] and not overwrite:
            continue

        try:
            output = util.command_output(command + [util.syspath(item.path)]).stdout
        except (subprocess.CalledProcessError, OSError) as exc:
            self._log.error("execution failed: {0}", exc)
            continue
        except UnicodeEncodeError:
            # Workaround for Python 2 Windows bug.
            # https://bugs.python.org/issue1759845
            self._log.error("execution failed for Unicode path: {0!r}", item.path)
            continue

        try:
            key_raw = output.rsplit(None, 1)[-1]
        except IndexError:
            # Sometimes keyfinder-cli returns 0 but with no key, usually
            # when the file is silent or corrupt, so we log and skip.
            self._log.error("no key returned for path: {0}", item.path)
            continue

        try:
            key = util.text_string(key_raw)
        except UnicodeDecodeError:
            self._log.error("output is invalid UTF-8")
            continue

        item["initial_key"] = key
        self._log.info(
            "added computed initial key {0} for {1}",
            key,
            util.displayable_path(item.path),
        )

        if write:
            item.try_write()
        item.store()