# Buggy code
def browse(self, uri):
    logger.debug("Browsing files at: %s", uri)
    result = []
    local_path = path.uri_to_path(uri)

    if str(local_path) == "root":
        return list(self._get_media_dirs_refs())

    if not self._is_in_basedir(local_path):
        logger.warning(
            "Rejected attempt to browse path (%s) outside dirs defined "
            "in file/media_dirs config.",
            uri,
        )
        return []

    for dir_entry in local_path.iterdir():
        child_path = dir_entry.resolve()
        uri = path.path_to_uri(child_path)

        if not self._show_dotfiles and dir_entry.name.startswith("."):
            continue

        if (
            self._excluded_file_extensions
            and dir_entry.suffix in self._excluded_file_extensions
        ):
            continue

        if child_path.is_symlink() and not self._follow_symlinks:
            logger.debug("Ignoring symlink: %s", uri)
            continue

        if not self._is_in_basedir(child_path):
            logger.debug("Ignoring symlink to outside base dir: %s", uri)
            continue

        if child_path.is_dir():
            result.append(models.Ref.directory(name=dir_entry.name, uri=uri))
        elif child_path.is_file():
            result.append(models.Ref.track(name=dir_entry.name, uri=uri))

    def order(item):
        return (item.type != models.Ref.DIRECTORY, item.name)

    result.sort(key=order)

    return result


# Traceback (NotADirectoryError)
FileBackend-2 DEBUG    2020-05-07 14:06:45,889 Browsing files at: file:///home/nick/Music/Tall%20Ships%20-%20Chemistry.mp3
Core-7 ERROR    2020-05-07 14:06:45,889 FileBackend backend caused an exception.
Traceback (most recent call last):
File "/home/nick/Dev/mopidy-dev/mopidy/mopidy/core/library.py", line 17, in _backend_error_handling
yield
File "/home/nick/Dev/mopidy-dev/mopidy/mopidy/core/library.py", line 114, in _browse
result = backend.library.browse(uri).get()
File "/usr/lib/python3/dist-packages/pykka/_threading.py", line 45, in get
_compat.reraise(*self._data['exc_info'])
File "/usr/lib/python3/dist-packages/pykka/_compat/__init__.py", line 29, in reraise
raise value
File "/usr/lib/python3/dist-packages/pykka/_actor.py", line 193, in _actor_loop
response = self._handle_receive(envelope.message)
File "/usr/lib/python3/dist-packages/pykka/_actor.py", line 299, in _handle_receive
return callee(*message.args, **message.kwargs)
File "/home/nick/Dev/mopidy-dev/mopidy/mopidy/file/library.py", line 55, in browse
for dir_entry in local_path.iterdir():
File "/usr/lib/python3.8/pathlib.py", line 1113, in iterdir
for name in self._accessor.listdir(self):
NotADirectoryError: [Errno 20] Not a directory: '/home/nick/Music/Tall Ships - Chemistry.mp3'

# Fixed code
def browse(self, uri):
    logger.debug("Browsing files at: %s", uri)
    result = []
    local_path = path.uri_to_path(uri)

    if str(local_path) == "root":
        return list(self._get_media_dirs_refs())

    if not self._is_in_basedir(local_path):
        logger.warning(
            "Rejected attempt to browse path (%s) outside dirs defined "
            "in file/media_dirs config.",
            uri,
        )
        return []
    if path.uri_to_path(uri).is_file():
        logger.error("Rejected attempt to browse file (%s)", uri)
        return []

    for dir_entry in local_path.iterdir():
        child_path = dir_entry.resolve()
        uri = path.path_to_uri(child_path)

        if not self._show_dotfiles and dir_entry.name.startswith("."):
            continue

        if (
            self._excluded_file_extensions
            and dir_entry.suffix in self._excluded_file_extensions
        ):
            continue

        if child_path.is_symlink() and not self._follow_symlinks:
            logger.debug("Ignoring symlink: %s", uri)
            continue

        if not self._is_in_basedir(child_path):
            logger.debug("Ignoring symlink to outside base dir: %s", uri)
            continue

        if child_path.is_dir():
            result.append(models.Ref.directory(name=dir_entry.name, uri=uri))
        elif child_path.is_file():
            result.append(models.Ref.track(name=dir_entry.name, uri=uri))

    def order(item):
        return (item.type != models.Ref.DIRECTORY, item.name)

    result.sort(key=order)

    return result