20
20
This line defines a function named count_file_content that takes one parameter,
file_path.
file_path is the path to the file whose content will be analyzed.
This line opens the file specified by file_path in read mode ('r') and assigns
the file object to the variable file.
The with open(...): statement ensures that the file is properly closed after
the block of code is executed.
lines = file.readlines()
This line reads all the lines from the file and stores them in the list lines.
Each element in the lines list is a string representing a single line from the
file, including the newline character at the end of each line.
num_lines = len(lines)
This line calculates the number of lines in the file by finding the length of
the lines list.
num_lines stores the total count of lines.
print(f"Lines: {num_lines}")
This line prints the total number of lines in the file using an f-string to
format the output.
print(f"Words: {num_words}")
This line prints the total number of words in the file using an f-string to
format the output.
print(f"Characters: {num_chars}")
This line prints the total number of characters in the file using an f-string
to format the output.
count_file_content("input.txt")
This line calls the count_file_content function with "input.txt" as the
argument.
It opens the file input.txt, counts the number of lines, words, and characters,
and prints these counts to the console.