Question: Write a Python function called lines _ of _ code that takes a Path object as a parameter, which is expected to be the path

Write a Python function called lines_of_code that takes a Path object as a parameter, which is expected to be the path to a Python script. It returns how many lines of Python code are contained within the script. Note that not all lines of text are considered to be "lines of code." We'll want to skip a few things, so we aren't counting lines that lack meaning; do not count lines that have these characteristics:
A line containing no text
A line containing nothing but spaces and/or tabs
A line whose only non-space or non-tab characters are part of a comment
(Note that when I refer to "comments" here, I'm referring only to Python comments, i.e., the portions of a line that begin with a # character. Some people use string literals as comments most notably, they use multiline string literals, because Python lacks multiline comments and a string literal "floating" in one's code turns out to be legal, though it's not a comment, because it still gets evaluated at run-time but these aren't comments, so you would want to count them.)
The function returns an integer that indicates how many lines of code are in the Python script.
If the file cannot be opened successfully, or if the file contains something other than text, the function should raise an exception, though it's not important what kind of exception is raised any exception will do, and it's fine if you raise different kinds of exceptions in different scenarios. However, the file is required to be closed in any case in which it had been opened.
What to submit
Submit one Python script named problem5.py, which contains your lines_of_code function. A docstring is not required, since we've all agreed already on what problem we're solving here.
The function and any additional functions you've written that it calls (if any) are all you need to write in the module; we don't need you to write automated tests and we haven't discussed in class how to do that, though you can, as long as importing the module can be done without generating any output or other effects, so that we can run automated tests of our own. (One way to meet that last requirement is for your automated tests to appear in a separate function, so they only run if you call it explicitly. If you're not sure how to meet this requirement, the safest thing is not to submit any automated tests; if we can't run our automated tests, you may not receive full credit here.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!