I have been working on this DNA problem for a while now, and I do not understand what causes the many problems. Right now there are just some technical problems, for example saying "No match" instead of "Lavender". In check50, there is a traceback error:

Traceback (most recent call last):
  File "/tmp/tmp7j4mzmff/test2/dna.py", line 83, in <module>
  File "/tmp/tmp7j4mzmff/test2/dna.py", line 18, in main
FileNotFoundError: [Errno 2] No such file or directory: '/workspaces/9..

Can you help?

import csv
import sys
import os

def main():
    if len(sys.argv) != 3:
        print("Usage: python dna.py data.csv sequence.txt")

    database = []
    with open(sys.argv[1], "r") as file:
        reader = csv.DictReader(file)
        for row in reader:

    path = "/workspaces/90389241/dna/sequences"
    for file in os.listdir():
        with open(file, 'r') as file:
            sequence = file.read()

# TODO: Find longest match of each STR in DNA sequence
    subsequences = list(database[0].keys())[1:]

    finals = {}
    for subsequence in subsequences:
        finals[subsequence] = longest_match(sequence, subsequence)

# TODO: Check database for matching profiles
    for someone in database:
        count2 = 0
        for subsequence in subsequences:
            if (int(someone[subsequence]) == finals[subsequence]):
                count2 += 1

        if count2 == len(subsequences):

        print("No match")

def longest_match(sequence, subsequence):
    """Returns length of longest run of subsequence in sequence."""

    # Initialize variables
    longest_run = 0
    subsequence_length = len(subsequence)
    sequence_length = len(sequence)

    # Check each character in sequence for most consecutive runs of subsequence
    for i in range(sequence_length):

        # Initialize count of consecutive runs
        count = 0

        # Check for a subsequence match in a "substring" (a subset of characters) within sequence
        # If a match, move substring to next potential match in sequence
        # Continue moving substring and checking for matches until out of consecutive matches
        while True:

            # Adjust substring start and end
            start = i + count * subsequence_length
            end = start + subsequence_length

            # If there is a match in the substring
            if sequence[start:end] == subsequence:
                count += 1

            # If there is no match in the substring

        # Update most consecutive matches found
        longest_run = max(longest_run, count)

    # After checking for runs at each character in seqeuence, return longest run found
    return longest_run


My first given error was, and I quote, "KeyError: 0" for "subsequences = list(database[0].keys())[1:]", but I don't think there is a problem with accessing the data inside of "database". I had used

if len(sys.argv[2]) == "databases/small.csv":
        with open("small.csv", "r") as file:
            reader = csv.DictReader(file)
            for row in reader:

    elif len(sys.argv[2]) == "databases/large.csv":
        with open("large.csv", "r") as file:
            reader = csv.DictReader(file)
            for row in reader:

instead of what I used above.

  • Please do not keep changing the problem/question or no one will help and the question will be marked closed. On this website questions are a single question it's not a dynamic exercise. One question, potential answers. Ask a specific question and stick with it. you can open a separate question for other questions. Please keep the questions limited to a minimally reproducible example. For example if you have a problem with os.chdir we only need an example where you try to do that exact bit, we don't need the whole program. learn to break a problem into subproblems important part of the course
    – UpAndAdam
    Commented Sep 2, 2023 at 15:24

1 Answer 1


The original error about the database key not found is telling you exactly what the problem is. Don't "think", know. It says the index 0 was not in your list database. I suggest you add a print statement before that line to print out that to confirm:

if ( len(database) > 0 ):
    print("database 0: ", str(database[0]))
    print("empty database")

note I've casted to a string just in case you do have integers in your database dictionary. If this gives you a key error than your database doesn't have anything in it.

For your second item I suggest that you check the directory structure carefully. It sounds like your path argument is not what is expected. Have you double checked that that path actually exists?

  • Sorry, "names" was the previous variable I used instead of "database". The problem is caused by the usage of "if" and "elif" as I also showed in my question. Commented Sep 1, 2023 at 19:40
  • so is the error still occuring? did it occur with both versions? unclear from your comment
    – UpAndAdam
    Commented Sep 5, 2023 at 14:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .