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