Debugging with the right number of command-line arguments

Question:

I want to debug my code that check for a certain number of command-line arguments; but when I hit step over it goes straight to another condition where the command line arguments are fewer. How do I debug my code when I have the right number of command-line argument?

Here is the code that I am trying to debug

import sys
import csv

# check for two command-line arguments
if len(sys.argv) == 3:
    try:
    # creat a buffer csv file
        buffer = []
        #open input file for reading
        with open(sys.argv[1]) as file:
            # read input file
            reader = csv.reader(file)
            for row in reader:
                # append each line into a csv buffer
                buffer.append(row)

        # open output file for writing
        with open(sys.argv[2], "w") as file_output:
            writer = csv.DictWriter(file_output, fieldnames= ["first", "last", "house"])
            # ignore the first row of csv buffer
            for line in buffer[1:]:
                # split contents of csv buffer
                line[0] = "first"
                line[1] = "last"
                line[2] = "house"
                # write into the output file
                writer.writerow({"first": first, "last": last, "house": house})

    except OSError:
        sys.exit(f"Couldn't read {sys.argv[1]}")




# check for fewer command-line arguments
elif len(sys.argv) < 3:
    sys.exit("Too few comman-line arguments")

# check for more command-line argument
else:
    sys.exit("Too many command-line arguments")

My code is not working as I expected; I am trying to debug it.

Asked By: Balup

||

Answers:

to debugging your code is to add print statements at various points in your code to track its execution and determine where the problem is occurring. For example, you could add the following print statements to your code

import sys
import csv

if len(sys.argv) == 3:
    print("Correct number of command-line arguments")
    try:
        # creat a buffer csv file
        buffer = []
        #open input file for reading
        with open(sys.argv[1]) as file:
            # read input file
            reader = csv.reader(file)
            for row in reader:
                # append each line into a csv buffer
                buffer.append(row)

        # open output file for writing
        with open(sys.argv[2], "w") as file_output:
            writer = csv.DictWriter(file_output, fieldnames= ["first", "last", "house"])
            # ignore the first row of csv buffer
            for line in buffer[1:]:
                # split contents of csv buffer
                line[0] = "first"
                line[1] = "last"
                line[2] = "house"
                # write into the output file
                writer.writerow({"first": first, "last": last, "house": house})
    except OSError:
        sys.exit(f"Couldn't read {sys.argv[1]}")

# check for fewer command-line arguments
elif len(sys.argv) < 3:
    print("Too few command-line arguments")
    sys.exit("Too few comman-line arguments")

# check for more command-line argument
else:
    print("Too many command-line arguments")
    sys.exit("Too many command-line arguments")
When you run your code, you can observe the output of the print statements to see where the problem is occurring and make adjustments as needed.

Additionally, you could use a debugger like pdb or ipdb to step through your code line by line and observe its behavior. This will give you more control and insight into the flow of your code and allow you to identify the source of the problem more easily

Answered By: OB Ayoub
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.