0

I have this assignment:

"In a file called lines.py, implement a program that expects exactly one command-line argument, the name (or path) of a Python file, and outputs the number of lines of code in that file, excluding comments and blank lines. If the user does not specify exactly one command-line argument, or if the specified file’s name does not end in .py, or if the specified file does not exist, the program should instead exit via sys.exit.

Assume that any line that starts with #, optionally preceded by whitespace, is a comment. (A docstring should not be considered a comment.) Assume that any line that only contains whitespace is blank."

And I wrote this python code:

###problem_set_6 --> lines.py
import sys
    
if len(sys.argv) == 2:        
    try:
        with open(sys.argv[1] , "r") as file:
            lines = file.readlines()
        Linesofcode = 0
    
        for line in lines:
            if line[0] == "#" or line.isspace():
                Linesofcode = Linesofcode + 1
    except FileNotFoundError:
        sys.exit("File does not exist")
    
    print(Linesofcode)
    
    
elif len(sys.argv) == 1:
    sys.exit("Too few command-line arguments")
elif ".py" not in sys.argv:
    sys.exit("Not a python file")
elif len(sys.argv) > 2:
    sys.exit("Too many command-line arguments")

Now when I try to submit this question in github, I get this error. I want to understand what does this error exactly mean in the case of my code and how to solve it in this particular case:-

lines.py yields 5 given a file with 5 lines, whitespace, and comments
        cant check until a frown is upside down
lines.py exits given more than one command-line argument
        cant check until a frown is upside down

2 Answers 2

1

There is another significant logical problem in your program, you are counting the number of lines that start with a space and the lines that are all spaces, when those are some of the lines you should NOT be counting.

Your own instructions say that you can assume any line that starts with a # or optionally with spaces in front of it is a comment and not to count comments, and not to count blank lines.

You need to count lines unless you the first character in the line that isn't a space is a # or if it is blank.

I suggest you put together a small test of this with a file that consists of the following:

# comment
 # comment
   # comment

line1
line2
line3
    
 
line4

and make sure your program gets a count of 4 as a beginning test. You could make it even easier and try individual lines of that and test to see if you get 0 or 1 first.

As Cliff points out "Cant check until a frown is upside down" means a previous test failed and no further tests are evaluated because they depend on that failed check to pass.

Additionally I would agree with cliff that before even opening the file you should check that it's a python file ( that the name ends in .py ) because you don't need to do any checking if it's the wrong kind of file. Currently you don't even check this issue if the correct number of arguments are provided. You don't want to bother opening and reading a file unless it's a .py file here.

I would also suggest you consider simplifying your general check for arguments. Capture sys.argv() into a variable lets say num_args, then if num_args != 2 then you know you have a problem and you're going to need to call sys.exit() otherwise you can proceed to check if the arg is a python file, if not again exit.. and if you haven't exited now you can finally count your lines.

1

"cant check until a frown is upside down" means that a previous test failed and further tests depend on that failed test to pass.

In other words, you need to fix that error before those other tests can execute.

looking at the code, it seems that the program will run before checking if the file ends in .py The program needs to do ALL of the validation checks before counting lines.

Now that you understand that issue, I'll give you a chance to fix the existing problems. ;-) If you can't, please post a new question with a specific issue.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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