# Buggy code
def choose_file(multiple: bool) -> List[str]:
    """Select file(s) for uploading, using external command defined in config.

    Args:
        multiple: Should selecting multiple files be allowed.

    Return:
        A list of selected file paths, or empty list if no file is selected.
        If multiple is False, the return value will have at most 1 item.
    """
    handle = tempfile.NamedTemporaryFile(prefix="qutebrowser-fileselect-", delete=False)
    handle.close()
    tmpfilename = handle.name
    with utils.cleanup_file(tmpfilename):
        if multiple:
            command = config.val.fileselect.multiple_files.command
        else:
            command = config.val.fileselect.single_file.command

        proc = guiprocess.GUIProcess(what="choose-file")
        proc.start(command[0], [arg.replace("{}", tmpfilename) for arg in command[1:]])

        loop = qtutils.EventLoop()
        proc.finished.connect(lambda _code, _status: loop.exit())
        loop.exec()

        try:
            with open(tmpfilename, mode="r", encoding=sys.getfilesystemencoding()) as f:
                selected_files = f.read().splitlines()
        except OSError as e:
            message.warning(f"Failed to open tempfile {tmpfilename} ({e})!")
            selected_files = []

    if not multiple:
        if len(selected_files) > 1:
            message.warning("More than one file chosen, using only the first")
            return selected_files[:1]
    return selected_files


# Traceback (FileNotFoundError)
20:03:45 DEBUG    procs      guiprocess:_pre_start:154 Executing: kitty nnn -p /tmp/qutebrowser-fileselect-kqiigxcd
[...]
20:03:48 DEBUG    procs      guiprocess:_on_finished:91 Process finished with code 0, status 0.
20:03:48 ERROR    misc       utils:cleanup_file:891 Failed to delete tempfile /tmp/qutebrowser-fileselect-kqiigxcd ([Errno 2] No such file or directory: '/tmp/qutebrowser-fileselect-kqiigxcd')!
20:03:48 ERROR    misc       crashsignal:_handle_early_exits:240 Uncaught exception
Traceback (most recent call last):
File "/usr/lib/python3.9/site-packages/qutebrowser/browser/webengine/webview.py", line 255, in chooseFiles
return shared.choose_file(
File "/usr/lib/python3.9/site-packages/qutebrowser/browser/shared.py", line 375, in choose_file
with open(tmpfilename, mode='r', encoding=sys.getfilesystemencoding()) as f:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/qutebrowser-fileselect-kqiigxcd'

# Fixed code
def choose_file(multiple: bool) -> List[str]:
    """Select file(s) for uploading, using external command defined in config.

    Args:
        multiple: Should selecting multiple files be allowed.

    Return:
        A list of selected file paths, or empty list if no file is selected.
        If multiple is False, the return value will have at most 1 item.
    """
    handle = tempfile.NamedTemporaryFile(prefix="qutebrowser-fileselect-", delete=False)
    handle.close()
    tmpfilename = handle.name
    with utils.cleanup_file(tmpfilename):
        if multiple:
            command = config.val.fileselect.multiple_files.command
        else:
            command = config.val.fileselect.single_file.command

        proc = guiprocess.GUIProcess(what="choose-file")
        proc.start(command[0], [arg.replace("{}", tmpfilename) for arg in command[1:]])

        loop = qtutils.EventLoop()
        proc.finished.connect(lambda _code, _status: loop.exit())
        loop.exec()

        try:
            with open(tmpfilename, mode="r", encoding=sys.getfilesystemencoding()) as f:
                selected_files = f.read().splitlines()
        except OSError as e:
            message.error(f"Failed to open tempfile {tmpfilename} ({e})!")
            selected_files = []

    if not multiple:
        if len(selected_files) > 1:
            message.warning("More than one file chosen, using only the first")
            return selected_files[:1]
    return selected_files