Program to count the total number of consonants in a file

In this python file program, we will count the total number of consonants in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.

				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the data and split it into words using file.read().split().
  3. Create a list of vowels.
  4. Create a count variable and assign its value equal to 0.
  5. Use a for loop to iterate over the words.
  6. Use a nested for loop to iterate over the characters in the word.
  7. Check whether the character is not in the vowels list using an if statement.
  8. If yes then add 1 to the count variable.
  9. Print the output.
				
					# Open file
file = open('readcontent.txt')
# Read data and converting into words
words = file.read().split()
# Create a list of vowels
vowels = ['a','e','i','o','u','A','E','I','O','U']
# Create count variable
count = 0
# Iterate over words
for word in words:
# Iterate over characters in the word
    for char in word:
    # Check for consonants
        if char not in vowels:
    # Add 1 to the count variable for each consonant
            count += 1
# Print output
print("Total number of consonants in the file: ",count)
				
			

Output :

Total number of consonants in the file: 48

 

 

count the total number of vowels in a file.

remove all the lines that contain the character ‘t’ in a file and write it to another file.

Program to count the total number of vowels in a file

In this python file program, we will count the total number of vowels in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.

				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the data and split it into words using file.read().split().
  3. Create a list of vowels.
  4. Create a count variable and assign its value equal to 0.
  5. Use a for loop to iterate over the words.
  6. Use a nested for loop to iterate over the characters in the word.
  7. Check whether the character is in the vowels list using an if statement.
  8. If yes then add 1 to the count variable.
  9. Print the output.
				
					# Open file
file = open('readcontent.txt')
# Read data and converting it into words
words = file.read().split()
# Create list of vowels
vowels = ['a','e','i','o','u','A','E','I','O','U']
# Create count variable
count = 0
# Iterate over words
for word in words:
# Iterate over characters in the word
    for char in word:
    # Check for vowels
        if char in vowels:
    # Add 1 to the count variable for each vowel
            count += 1
# Print output
print("Total number of vowels in the file: ",count)
				
			

Output :

Total number of vowels in the file: 31

 

 

count the total number of consonants in a file.

Program to read the content of the file in reverse order

In this python file program, we will read the content of the file in reverse order with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.

				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the data in the file using read().
  3. Convert it to a list using list().
  4. Now reverse the list using reversed().
  5. Use a for loop to iterate over the lines in the reversed list and print the line.
				
					# Open file
file = open('readcontent.txt')
# Read lines and converting it to a list
data = list(file.readlines())
# Iterate over lines in reverse order
for line in reversed(data):
    print(line)
				
			

Output :

Line4 : This is Australia.

Line3 : This is Canada.

Line2 : This is America.

Line1 : This is India.




move the cursor to a specific position in a file.

Program to move the cursor to a specific position in a file

In this python file program, we will move the cursor to a specific position in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.

				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the lines using readlines().
  3. Print the position of the cursor using file.tell()
  4. Move the cursor to the 10th position using file.seek(10).
  5. Again print the position of the cursor using file.tell().
				
					# Open the file
file = open('readcontent.txt')
# Read lines in the file
file.readline()
# Print position of the curosr
print("Position of a cursor in the file: ",file.tell())
# Move cursor to a specific Position
file.seek(10)
# Again print position of the cursor
print("Position of a cursor in the file: ",file.tell())
				
			

Output :

Position of a cursor in the file: 23 
Position of a cursor in the file: 10


 

find the cursor position in a file.

read the content of the file in reverse order.

Program to find the cursor position in a file

In this python file program, we will find the cursor position in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.

				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the lines using readlines().
  3. Print the position of the cursor using file.tell()
				
					# Open the file
file = open('readcontent.txt')
# Read lines of the file
file.readline()
# Print the position of the cursor
print("Position of a cursor in the file: ",file.tell())
				
			

Output :

Position of a cursor in the file: 23

 

 

count the total number of special characters in a file.

move the cursor to a specific position in a file.

Program to count the total number of special characters in a file

In this python file program, we will count the total number of special characters in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is @ndia.
Line2 : This is $America.
Line3 : This is Canada?.
Line4 : This is #Australia.

				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the data and split it into words using file.read().split().
  3. Create a count variable and assign its value equal to 0.
  4. Create a list of special characters
  5. Use a for loop to iterate over the words.
  6. Use a nested for loop to iterate over the characters in the word.
  7. If the character is in the list then add 1 to the count variable.
  8. Print the output.
				
					# Open the file
file = open('readcontent.txt')
# Read data and converting it into words
words = file.read().split()
# Create count variable
count = 0
# Create list of special characters
special = ['!','@','#','$','%','^','&','*',
          '~','`','?',':',';']
# Iterate over words
for word in words:
# Iterate over characters in the word
    for char in word:
    # Check for special characters
        if char in special:
    # Add 1 to the count variable for each special character
            count += 1
# Print output
print("Total number of digits in the file: ",count)
				
			

Output :

Total number of digits in the file: 8

 

 

count the total number of digits in a file.

find the cursor position in a file.

Program to count the total number of digits in a file

In this python file program, we will count the total number of digits in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the data and split it into words using file.read().split().
  3. Create a count variable and assign its value equal to 0.
  4. Use a for loop to iterate over the words.
  5. Use a nested for loop to iterate over the characters in the word.
  6. Check if the character is a number or not using isnumeric().
  7. If yes then add 1 to the count variable.
  8. Print the output.
				
					# Open the file
file = open('readcontent.txt')
# Read data and converting it into words
words = file.read().split()
# Create count variable
count = 0
# Iterate over words
for word in words:
# Iterate over characters in the word
    for char in word:
    # Check for digits
        if char.isnumeric():
    # Add 1 to the count variable for each digit
            count += 1
# Print output
print("Total number of lower case characters in the file: ",count)
				
			

Output :

Total number of digits in the file: 4

 

 

count the total number of Lowercase characters in a file.

count the total number of special characters in a file.

Program to count the total number of Lowercase characters in a file

In this python file program, we will count the total number of Lowercase characters in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the data and split it into words using file.read().split().
  3. Create a count variable and assign its value equal to 0.
  4. Use a for loop to iterate over the words.
  5. Use a nested for loop to iterate over the characters in the word.
  6. Check if the character is lowercase or not using islower().
  7. If yes then add 1 to the count variable.
  8. Print the output.
				
					# Open the file
file = open('readcontent.txt')
# Read data and converting it into words
words = file.read().split()
# Create count variable
count = 0
# Iterate over words
for word in words:
# Iterate over characters of the words
    for char in word:
    # Check for lowercase characters
        if char.islower():
    # Add 1 to the count variable for each lowercase character
            count += 1
# Print output
print("Total number of lower case characters in the file: ",count)
				
			

Output :

Total number of lower case characters in the file: 55

 

 

count the total number of Uppercase characters in a file.

count the total number of digits in a file.

Program to count the total number of Uppercase characters in a file

In this python file program, we will count the total number of Uppercase characters in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the data and split it into words using file.read().split().
  3. Create a count variable and assign its value equal to 0.
  4. Use a for loop to iterate over the words.
  5. Use a nested for loop to iterate over the characters in the word.
  6. Check if the character is uppercase or not using isupper().
  7. If yes then add 1 to the count variable.
  8. Print the output.
				
					# Open file
file = open('readcontent.txt')
# Read data and converting it into words
words = file.read().split()
# Create count variable
count = 0
# Iterate over words
for word in words:
# Iterate over characters in the words
    for char in word:
    # Check for lowercase characters
        if char.isupper():
    # Add 1 to the count varibale for each uppercase character
            count += 1
# Print output
print("Total number of upper case characters in the file: ",count)
				
			

Output :

Total number of upper case characters in the file: 12

 

 

count the total number of characters in a file.

count the total number of Lowercase characters in a file.

Count the total number of characters in a text file

In this python file program, we will count the total number of characters in a text file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Read the data and split it into words using file.read().split().
  3. Create a count variable and assign its value equal to 0.
  4. Use a for loop to iterate over the words.
  5. Use a nested for loop to iterate over the characters in the word.
  6. Use an if statement to check whether the character is a alphabet or not.
  7. After each iteration add 1 to the count variable.
  8. Print the output.
				
					# Open the file
file = open('readcontent.txt')
# Read the file and converting data into words
words = file.read().split()
# Create a count variable
count = 0
# Iterate over words
for word in words:
# Iterate over characters in the word
    for char in word:
    # Check for alphabet
        if char.isalpha():
    # Add 1 to the count varibale for each alphabet
            count += 1
# Print output
print("Total number of characters in the file: ",count)
				
			

Output :

Total number of characters in the file: 67

 

 

read the data of two of the files created and add it to a new file.

count the total number of Uppercase characters in a file.