Python – Get number of characters, words, spaces and lines in a file
Last Updated :
28 Aug, 2023
Given a text file fname, the task is to count the total number of characters, words, spaces, and lines in the file.
Python – Get number of characters, words, spaces and lines in a file
As we know, Python provides multiple in-built features and modules for handling files. Let’s discuss different ways to calculate the total number of characters, words, spaces, and lines in a file using Python.
Get the number of characters, words, spaces, and lines in a file
Get the number of characters, words, spaces, and lines in a file
Below are the methods that we will cover in this article:
- The naive approach of getting the number of characters, words, spaces, and lines in a file.
- Using some built-in functions and OS module functions
Count the Number of Lines, Words, and Characters using the Native Method
In this approach, the idea is to solve the task by developing our own logic. Without using any built-in function of Python, the total number of characters, words, spaces and lines of the file will be calculated. Below is the implementation of the above approach.
Python3
def counter(fname):
num_words = 0
num_lines = 0
num_charc = 0
num_spaces = 0
with open (fname, 'r' ) as f:
for line in f:
num_lines + = 1
word = 'Y'
for letter in line:
if (letter ! = ' ' and word = = 'Y' ):
num_words + = 1
word = 'N'
elif (letter = = ' ' ):
num_spaces + = 1
word = 'Y'
for i in letter:
if (i ! = " " and i ! = "\n" ):
num_charc + = 1
print ( "Number of words in text file: " ,
num_words)
print ( "Number of lines in text file: " ,
num_lines)
print ( 'Number of characters in text file: ' ,
num_charc)
print ( 'Number of spaces in text file: ' ,
num_spaces)
if __name__ = = '__main__' :
fname = 'File1.txt'
try :
counter(fname)
except :
print ( 'File not found' )
|
Output:
Number of words in text file: 25
Number of lines in text file: 4
Number of characters in text file: 91
Number of spaces in text file: 21
Time complexity: O(n) where n is the number of characters in the file.
Space complexity: O(1) as only a few variables are used to store the count and no additional data structures are used.
Find the number of characters in a file using Python
In this approach, the idea is to use the os.linesep() method of OS module to separate the lines on the current platform. When the interpreter’s scanner encounter os.linesep it replaces it with \n character. After that strip() and split() functions will be used to carry out the task. Get more ideas about strip() and split() functions. Below is the implementation of the above approach.
Python3
import os
def counter(fname):
num_words = 0
num_lines = 0
num_charc = 0
num_spaces = 0
with open (fname, 'r' ) as f:
for line in f:
line = line.strip(os.linesep)
wordslist = line.split()
num_lines = num_lines + 1
num_words = num_words + len (wordslist)
num_charc = num_charc + sum ( 1 for c in line
if c not in (os.linesep, ' ' ))
num_spaces = num_spaces + sum ( 1 for s in line
if s in (os.linesep, ' ' ))
print ( "Number of words in text file: " ,
num_words)
print ( "Number of lines in text file: " ,
num_lines)
print ( "Number of characters in text file: " ,
num_charc)
print ( "Number of spaces in text file: " ,
num_spaces)
if __name__ = = '__main__' :
fname = 'File1.txt'
try :
counter(fname)
except :
print ( 'File not found' )
|
Output:
Number of words in text file: 25
Number of lines in text file: 4
Number of characters in text file: 91
Number of spaces in text file: 21
Time complexity: O(n), where n is the number of characters in the file. The code reads the file line by line and performs operations on each line, so the time complexity grows linearly with the size of the file.
Space complexity: O(1), as the code only uses a constant amount of memory to store the count variables, regardless of the size of the file.
Please Login to comment...