I don't understand why my code fails when I run check50. When I run the code, it produces the expected results. However, when I run check50 I get this error message: Why?

:( scourgify.py creates new CSV file
    expected exit code 0, not 1

Here is my code:

import sys
import csv
def main():
    if len(sys.argv) < 3:
        print ("Too few command-line arguments")
    elif len(sys.argv) > 3:
        print ("Too many command-line arguments")
    elif len(sys.argv) == 3:
        str1 = sys.argv[1]
        str2 = sys.argv[2]
        if str1[-4::] != ".csv":
            print ("Not a CSV file")
        elif str2[-4::] != ".csv":
            print ("Not a CSV file")
                infile = open(sys.argv[1], "r")
            except FileNotFoundError:
                print ("Could not read invalid_file.csv")
            with open(sys.argv[2], 'w', newline='') as csvfile:
                fieldnames = ["first", "last", "house"]
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            #except FileNotFoundError:
                #print ("Could not read invalid_file.csv")
            #outfile = []
                for line in infile:
                    row = {}
                    first, last, house = line.rstrip().split(",")
                    row["first"] = first.strip('"')
                    row["last"] = last.lstrip().rstrip('"')
                    row["house"] = house
  • Please edit the question and format the code. An easy way is to choose Edit under the question, select the code and click {} in the format bar. Commented Jan 18, 2023 at 11:58
  • Thank you for the suggestion. i just feel like i resolved the problem but i still failed the check50. And i don't quite understand why.
    – JHo
    Commented Jan 19, 2023 at 3:29
  • This program does not run. After removing the last * ( paste problem perhaps?), it gives a value error using the supplied before.csv file. Have you run this locally to verify it gives correct result? That should be done before check50. Commented Jan 19, 2023 at 10:58
  • Thanks for your suggestion. the * was not in my original code, a copy and paste problem. i ran it locally and it created an output file with the information that i expected. So, i didn't know what went wrong.
    – JHo
    Commented Jan 19, 2023 at 15:44
  • 1
    When I run this with the before.csv provided with the pset instructions, I get an error executing this statement: first, last, house = line.rstrip().split(",") with the first line of data. The text on that line is name,house so it only returns 2 values when 3 are expected (first, last, house).
    – kcw78
    Commented Jan 19, 2023 at 16:53


You must log in to answer this question.

Browse other questions tagged .