![]() |
VOOZH | about |
Given a text file, the task is to count the total number of characters, words, spaces and lines in the file.
Example: (Myfile.txt)
Hello world
This is Python
It is easy
Output:
Lines: 3
Words: 8
Characters: 30
Spaces: 5
This is the most readable and Pythonic method. It uses split(), generator expressions and basic iteration.
Output
Lines: 3
Words: 8
Characters: 30
Spaces: 5
Explanation:
This method uses os.linesep, strip() and comprehension-based counting.
Output
Lines: 3
Words: 8
Characters: 30
Spaces: 5
Explanation:
This method manually checks every character and updates counters. It is more complex and less readable.
Output
Lines: 3
Words: 8
Characters: 30
Spaces: 5