Open In App

Python – Get number of characters, words, spaces and lines in a file

Last Updated : 28 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 number of characters, words, spaces and lines in a file

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




# Function to count number of characters, words, spaces and lines in a file
def counter(fname):
 
    # variable to store total word count
    num_words = 0
     
    # variable to store total line count
    num_lines = 0
     
    # variable to store total character count
    num_charc = 0
     
    # variable to store total space count
    num_spaces = 0
     
    # opening file using with() method
    # so that file gets closed
    # after completion of work
    with open(fname, 'r') as f:
         
        # loop to iterate file
        # line by line
        for line in f:
             
            # incrementing value of num_lines with each
            # iteration of loop to store total line count
            num_lines += 1
             
            # declaring a variable word and assigning its value as Y
            # because every file is supposed to start with a word or a character
            word = 'Y'
             
            # loop to iterate every
            # line letter by letter
            for letter in line:
                 
                # condition to check that the encountered character
                # is not white space and a word
                if (letter != ' ' and word == 'Y'):
                     
                    # incrementing the word
                    # count by 1
                    num_words += 1
                     
                    # assigning value N to variable word because until
                    # space will not encounter a word can not be completed
                    word = 'N'
                     
                # condition to check that the encountered character is a white space
                elif (letter == ' '):
                     
                    # incrementing the space
                    # count by 1
                    num_spaces += 1
                     
                    # assigning value Y to variable word because after
                    # white space a word is supposed to occur
                    word = 'Y'
                     
                # loop to iterate every character
                for i in letter:
                     
                    # condition to check white space
                    if(i !=" " and i !="\n"):
                         
                        # incrementing character
                        # count by 1
                        num_charc += 1
                         
    # printing total word count
    print("Number of words in text file: ",
          num_words)
     
    # printing total line count
    print("Number of lines in text file: ",
          num_lines)
     
    # printing total character count
    print('Number of characters in text file: ',
          num_charc)
     
    # printing total space count
    print('Number of spaces in text file: ',
          num_spaces)
     
# Driver Code:
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
 
# Function to count number
# of characters, words, spaces
# and lines in a file
def counter(fname):
     
    # variable to store total word count
    num_words = 0
     
    # variable to store total line count
    num_lines = 0
     
    # variable to store total character count
    num_charc = 0
     
    # variable to store total space count
    num_spaces = 0
     
    # opening file using with() method
    # so that file gets closed
    # after completion of work
    with open(fname, 'r') as f:
         
        # loop to iterate file
        # line by line
        for line in f:
             
            # separating a line from \n character
            # and storing again in line
            line = line.strip(os.linesep)
             
            # splitting the line
            wordslist = line.split()
             
            # incrementing value of num_lines
            num_lines = num_lines + 1
             
            # incrementing value of num_word
            num_words = num_words + len(wordslist)
             
            # incrementing value of num_char
            num_charc = num_charc + sum(1 for c in line
                        if c not in (os.linesep, ' '))
             
            # incrementing value of num_space
            num_spaces = num_spaces + sum(1 for s in line
                                if s in (os.linesep, ' '))
     
    # printing total word count
    print("Number of words in text file: ",
          num_words)
     
    # printing total line count
    print("Number of lines in text file: ",
          num_lines)
     
    # printing total character count
    print("Number of characters in text file: ",
          num_charc)
     
    # printing total space count
    print("Number of spaces in text file: ",
          num_spaces)
 
# Driver Code:
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.



Previous Article
Next Article

Similar Reads

Regex in Python to put spaces between words starting with capital letters
Given an array of characters, which is basically a sentence. However, there is no space between different words and the first letter of every word is in uppercase. You need to print this sentence after the following amendments: Put a single space between these words. Convert the uppercase letters to lowercase Examples: Input : BruceWayneIsBatmanOut
2 min read
Python Program to Count Vowels, Lines, Characters in Text File
In this article, we are going to create a python program that counts vowels, lines, and a number of characters present in a particular text file. ApproachWe have to open the file using open() function in python.Then make three variables, vowel, line and character to count the number of vowels, lines, and characters respectively.Make a list of vowel
2 min read
Python program to count the number of blank spaces in a text file
All programs need input to process and after processing gives the output. Python support file handling and allow user to handle files. The concept of file handling has stretched over various other languages, But the implementation is either lengthy or complicated. Python treats file differently as text and binary,One thing to note while writing dat
3 min read
Python - Convert simple lines to bulleted lines using the Pyperclip module
Pyperclip is the cross-platform Python module which is used for copying and pasting the text to the clipboard. Let's suppose you want to automate the task of copying the text and then convert them to the bulleted points. This can be done using the pyperclip module. Consider the below examples for better understanding. Examples: Clipboard Content :
2 min read
Python - Compute the frequency of words after removing stop words and stemming
In this article we are going to tokenize sentence, paragraph, and webpage contents using the NLTK toolkit in the python environment then we will remove stop words and apply stemming on the contents of sentences, paragraphs, and webpage. Finally, we will Compute the frequency of words after removing stop words and stemming. Modules Needed bs4: Beaut
8 min read
Count number of lines in a text file in Python
Counting the number of characters is important because almost all the text boxes that rely on user input have a certain limit on the number of characters that can be inserted. For example, If the file is small, you can use readlines() or a loop approach in Python. Input: line 1 line 2 line 3 Output: 3 Explanation: The Input has 3 number of line in
3 min read
How to count the number of lines in a CSV file in Python?
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the n
2 min read
Python program to calculate the number of words and characters in the string
Given a String. The task is to find out the Number Of Words And Characters Present In The String. Examples: Input: Geeksforgeeks is best Computer Science Portal Output: The number Of Words are : 6 The Number Of Characters are : 45 Input: Hello World!!! Output: The original string is : Hello World!!! The number of words in string are : 2 The number
4 min read
How To Get The Uncompressed And Compressed File Size Of A File In Python
We are given a compressed file as well as uncompressed file. Our task is to get the size of uncompressed as well as compressed file in Python. In this article, we will see how we can get the uncompressed and compressed file size of a file in Python. What is Uncompressed And Compressed File?Uncompressed File: An uncompressed file is a file that has
3 min read
Python - Get file id of windows file
File ID is a unique file identifier used on windows to identify a unique file on a Volume. File Id works similar in spirit to a inode number found in *nix Distributions. Such that a fileId could be used to uniquely identify a file in a volume. We would be using an command found in Windows Command Processor cmd.exe to find the fileid of a file. In o
3 min read
three90RightbarBannerImg