Correct option is A
The code uses the readlines() method to read all lines from the file "NOTES.TXT" and store them in the list LINE. To find the number of lines in the file, you can use the len() function, which returns the number of elements in the list LINE.
Since readlines() reads each line from the file as an element in the list (including the newline character \n at the end of each line), the length of the list will represent the number of lines in the file.
Thus, the correct statement to display the number of lines is:
print(len(LINE))
Important Key Points:
1.
readlines(): This method reads the entire file, splitting it into lines and returning them as a list of strings.
2.
len(): This function returns the number of items (lines in this case) in the list, giving the total number of lines in the file.
3.
List Representation: Each element in the list corresponds to one line in the file, so the length of the list is the number of lines.
Knowledge Booster:
·
Option (b):
LINE.count() is incorrect because the count() method requires an argument (an element to count), and this is not how the number of lines is calculated.
·
Option (c):
LINE.count('\n') is incorrect because count('\n') would count the occurrences of newline characters within a single string or a list of strings. While it's related to line breaks, it's not the correct way to count the number of lines directly in the list.
·
Option (d):
LINE.len() is incorrect because there is no len() method associated with lists in Python. The correct function is the built-in len() function.