# Buggy code
def _local_inventory(self, cache):
    # for local files, perform BFS via os.scandir to determine existence of files
    if cache.remaining_wait_time <= 0:
        # No more time to create inventory.
        return

    start_time = time.time()

    root = cache.get_inventory_root(self)
    if root == self:
        # there is no root directory that could be used
        return
    if os.path.exists(root):
        queue = [root]
        while queue:
            path = queue.pop(0)
            # path must be a dir
            cache.exists_local[path] = True
            with os.scandir(path) as scan:
                for entry in scan:
                    if entry.is_dir():
                        queue.append(entry.path)
                    else:
                        # path is a file
                        cache.exists_local[entry.path] = True
            cache.remaining_wait_time -= time.time() - start_time
            if cache.remaining_wait_time <= 0:
                # Stop, do not mark inventory as done below.
                # Otherwise, we would falsely assume that those files
                # are not present.
                return

    cache.has_inventory.add(root)


# Traceback (FileNotFoundError)
Building DAG of jobs...
Traceback (most recent call last):
File "<base_path>/software/miniconda/envs/snakemake/lib/python3.7/site-packages/snakemake/__init__.py", line 709, in snakemake
keepincomplete=keep_incomplete,
File "<base_path>/software/miniconda/envs/snakemake/lib/python3.7/site-packages/snakemake/workflow.py", line 670, in execute
dag.init()
File "<base_path>/software/miniconda/envs/snakemake/lib/python3.7/site-packages/snakemake/dag.py", line 177, in init
job = self.update(self.file2jobs(file), file=file, progress=progress)
File "<base_path>/software/miniconda/envs/snakemake/lib/python3.7/site-packages/snakemake/dag.py", line 715, in update
progress=progress,
File "<base_path>/software/miniconda/envs/snakemake/lib/python3.7/site-packages/snakemake/dag.py", line 792, in update_
file.inventory()
File "<base_path>/software/miniconda/envs/snakemake/lib/python3.7/site-packages/snakemake/io.py", line 210, in inventory
self._local_inventory(cache)
File "<base_path>/software/miniconda/envs/snakemake/lib/python3.7/site-packages/snakemake/io.py", line 224, in _local_inventory
with os.scandir(path) as scan:
FileNotFoundError: [Errno 2] No such file or directory: '<analysis_patch>/tmp_batch113/codons'

# Fixed code
def _local_inventory(self, cache):
    # for local files, perform BFS via os.scandir to determine existence of files
    # obtaining mtime and size of the files is deferred for parallel execution
    # as this can be a slow step on network filesystems
    path = self.inventory_root

    if not os.path.exists(path):
        cache.has_inventory.add(path)
        return

    start = time.time()

    logger.debug("Inventory started of {}".format(path))
    pbuffer = []
    counter = 0
    try:
        for entry in os.scandir(path):
            is_file = self._local_inventory_direntry_quick(cache, entry)

            if is_file is True:
                counter += 1
                pbuffer.append(entry.path)
                if len(pbuffer) > 100:
                    cache.submit(self._local_inventory_path_complete, pbuffer)
                    pbuffer = []

        if pbuffer:
            cache.submit(self._local_inventory_path_complete, pbuffer)

        cache.has_inventory.add(path)
        logger.debug(
            "Inventory of {} completed in {:.1f} seconds. {} files added to stat queue ({} tasks in queue).".format(
                path, time.time() - start, counter, cache.queue.qsize()
            )
        )

    except OSError as e:
        logger.debug(
            "Inventory of {} failed. Continuing without inventory caching. Error message: {}.".format(
                path, str(e)
            )
        )