Task: Write a Python File Program To Count Characters in Text File. This Python Programming tutorial will explain a simple logic to show number of characters in a text file. The Python programming language provides open() function. We will use open() function to open an existing file in read mode.
As we know that the name of the text file is mytext.txt and it resides on D drive, according to the given question.
We will use another function read() to read all the contents of this text file and store them in a string variable ‘str1’. The len() function will calculate the number of characters in this string. Sine the string contains all data from the text file, so length of string will give the length of file data as well.
Assume that mytext.txt file has the following contents without quotes:
“This file contains some example text.”
The Source code of Python File Program To Count Characters in Text File
The output will be:
The Number of characters in text file : 37
The Python program wwithout comments
You may also like to read:
Python File Programs – Python File Handling
Write a Python file program read numbers write squares.
Python Text File Read and Show Contents Program
Python Text File Read and Display file data with Exception Handling FileNotFoundError
Python File Program Count Characters in Text File
Python Text File Program To Count Vowels