# Buggy code
def finished(self, job, keep_metadata=True):
    if not keep_metadata:
        for f in job.expanded_output:
            self._delete_record(self._incomplete_path, f)
        return

    version = str(job.rule.version) if job.rule.version is not None else None
    code = self._code(job.rule)
    input = self._input(job)
    log = self._log(job)
    params = self._params(job)
    shellcmd = job.shellcmd
    conda_env = self._conda_env(job)
    fallback_time = time.time()
    for f in job.expanded_output:
        rec_path = self._record_path(self._incomplete_path, f)
        starttime = os.path.getmtime(rec_path) if os.path.exists(rec_path) else None
        endtime = f.mtime if os.path.exists(f) else fallback_time
        self._record(
            self._metadata_path,
            {
                "version": version,
                "code": code,
                "rule": job.rule.name,
                "input": input,
                "log": log,
                "params": params,
                "shellcmd": shellcmd,
                "incomplete": False,
                "starttime": starttime,
                "endtime": endtime,
                "job_hash": hash(job),
                "conda_env": conda_env,
                "container_img_url": job.container_img_url,
            },
            f,
        )
        self._delete_record(self._incomplete_path, f)


# 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 finished(self, job, keep_metadata=True):
    if not keep_metadata:
        for f in job.expanded_output:
            self._delete_record(self._incomplete_path, f)
        return

    version = str(job.rule.version) if job.rule.version is not None else None
    code = self._code(job.rule)
    input = self._input(job)
    log = self._log(job)
    params = self._params(job)
    shellcmd = job.shellcmd
    conda_env = self._conda_env(job)
    fallback_time = time.time()
    for f in job.expanded_output:
        rec_path = self._record_path(self._incomplete_path, f)
        starttime = os.path.getmtime(rec_path) if os.path.exists(rec_path) else None
        endtime = f.mtime.local_or_remote() if f.exists else fallback_time
        self._record(
            self._metadata_path,
            {
                "version": version,
                "code": code,
                "rule": job.rule.name,
                "input": input,
                "log": log,
                "params": params,
                "shellcmd": shellcmd,
                "incomplete": False,
                "starttime": starttime,
                "endtime": endtime,
                "job_hash": hash(job),
                "conda_env": conda_env,
                "container_img_url": job.container_img_url,
            },
            f,
        )
        self._delete_record(self._incomplete_path, f)