1

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")
        sys.exit(1)
    elif len(sys.argv) > 3:
        print ("Too many command-line arguments")
        sys.exit(1)
    elif len(sys.argv) == 3:
        str1 = sys.argv[1]
        str2 = sys.argv[2]
        if str1[-4::] != ".csv":
            print ("Not a CSV file")
            sys.exit(1)
        elif str2[-4::] != ".csv":
            print ("Not a CSV file")
            sys.exit(1)
        else:
            try:
                infile = open(sys.argv[1], "r")
            except FileNotFoundError:
                print ("Could not read invalid_file.csv")
                sys.exit(1)
            with open(sys.argv[2], 'w', newline='') as csvfile:
                fieldnames = ["first", "last", "house"]
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                writer.writeheader()
            #except FileNotFoundError:
                #print ("Could not read invalid_file.csv")
                #sys.exit(1)
            #outfile = []
                for line in infile:
                    row = {}
                    first, last, house = line.rstrip().split(",")
                    row["first"] = first.strip('"')
                    row["last"] = last.lstrip().rstrip('"')
                    row["house"] = house
                    writer.writerow(row)
main()*
7
  • 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

0

You must log in to answer this question.

Browse other questions tagged .