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.

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

In this python file program, we will read the data of two of the files and add it to a new file with the help of the below-given steps. Let’s consider we have readcontent.txt1 and readcontent.txt2 files with the below content.

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

#readcontent.txt2
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
				
			
Steps to solve the program
  1. Create two variables and assign their values equal to an empty string.
  2. Open the first file using open(‘readcontent1.txt’) as f.
  3. Read and store the data in the first file in the first variable using read().
  4. Open the second file using open(‘readcontent2.txt’) as f.
  5. Read and store the data in the second file in the first variable using read().
  6. Add the new line at the end of the data in the first variable.
  7. Now add the data in the second variable to the first variable.
  8. Open the third file using open (‘writecontent.txt’, ‘w’) as f.
  9. Add the merged data to it using write().
				
					# Create two empty strings
data = data2 = ""
# Open first file
with open('readcontent1.txt') as f:
# Read the data
    data = f.read()
# Open second file
with open('readcontent2.txt') as f:
# Read the data
    data2 = f.read()
# Add a new line
data += "\n"
# Combine data
data += data2
# Open third file in write mode and write the new data to it
with open ('writecontent.txt', 'w') as f:
    f.write(data)
				
			

Output : Open the writecontent.txt file to see the result

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

Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.

 

 

extract characters from a text file into a list.

count the total number of characters in a file.

Program to extract characters from a text file into a list

In this python file program, we will extract characters from a text file into a list with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”).
  2. Where the file name is the name of the file.
  3. Read the data and split it into words using file.read().split().
  4. Create an empty list to store the characters in the file.
  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. Add the characters to the list using append().
  8. Print the output.
				
					# Open file in read mode
file = open('readcontent.txt')
# Read data and converting it into words
words = file.read().split()
# Create an empty list
list1 = []
# Iterate over words
for word in words:
# Iterate over Characters of the words
    for char in word:
    # Extract characters to the list
        list1.append(char)
# Print output
print("Characters in the file in the list: ",list1)
				
			

Output :

Characters in the file in the list: [‘L’, ‘i’, ‘n’, ‘e’, ‘1’, ‘:’, ‘T’, ‘h’, ‘i’, ‘s’, ‘i’, ‘s’, ‘I’, ‘n’, ‘d’, ‘i’, ‘a’, ‘.’, ‘L’, ‘i’, ‘n’, ‘e’, ‘2’, ‘:’, ‘T’, ‘h’, ‘i’, ‘s’, ‘i’, ‘s’, ‘A’, ‘m’, ‘e’, ‘r’, ‘i’, ‘c’, ‘a’, ‘.’, ‘L’, ‘i’, ‘n’, ‘e’, ‘3’, ‘:’, ‘T’, ‘h’, ‘i’, ‘s’, ‘i’, ‘s’, ‘C’, ‘a’, ‘n’, ‘a’, ‘d’, ‘a’, ‘.’, ‘L’, ‘i’, ‘n’, ‘e’, ‘4’, ‘:’, ‘T’, ‘h’, ‘i’, ‘s’, ‘i’, ‘s’, ‘A’, ‘u’, ‘s’, ‘t’, ‘r’, ‘a’, ‘l’, ‘i’, ‘a’, ‘.’, ‘L’, ‘i’, ‘n’, ‘e’, ‘5’, ‘:’, ‘T’, ‘h’, ‘i’, ‘s’, ‘i’, ‘s’, ‘A’, ‘f’, ‘r’, ‘i’, ‘c’, ‘a’, ‘.’, ‘L’, ‘i’, ‘n’, ‘e’, ‘6’, ‘:’, ‘T’, ‘h’, ‘i’, ‘s’, ‘i’, ‘s’, ‘K’, ‘o’, ‘r’, ‘e’, ‘a’, ‘.’, ‘L’, ‘i’, ‘n’, ‘e’, ‘7’, ‘:’, ‘T’, ‘h’, ‘i’, ‘s’, ‘i’, ‘s’, ‘G’, ‘e’, ‘r’, ‘m’, ‘a’, ‘n’, ‘y’, ‘.’, ‘L’, ‘i’, ‘n’, ‘e’, ‘8’, ‘:’, ‘T’, ‘h’, ‘i’, ‘s’, ‘i’, ‘s’, ‘C’, ‘h’, ‘i’, ‘n’, ‘a’, ‘.’]

 

 

check whether a file is closed or not.

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

Program to check whether a file is closed or not

In this python file program, we will check whether a file is closed or not 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.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
				
			
Steps to solve the program
  1. Open the file by using open(“readcontent.txt”,”r”).
  2. Print file.closed, it will print True if the file is closed and False if not.
  3. Now close the file.
  4. Again use file.closed to check whether the file is closed or not.
				
					# Open file in read mode
f = open('readcontent.txt','r')
# Check whether the file is closed or not
print(f.closed)
# Close the file
f.close()
# Check whether the file is closed or not
print(f.closed)
				
			

Output :

False
True



write a tuple to a file.

extract characters from a text file into a list.

Python file program to write a tuple to a file

In this python file program, we will write a tuple to a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
				
			
Steps to solve the program
  1. Create a tuple.
  2. Open the file by using open(“readcontent.txt”,”w”).
  3. Where file name is the name of the file and w is writing mode.
  4. Inside write() use ” “.join(tup) to write the tuple in the file.
  5. Close the file.
				
					# Create a tuple
tup = ('1','2','3')
# Open file in write mode
f = open('readcontent.txt','w') 
# Write tuple to file
f.write(" ".join(tup))
# Close the file
f.close() 
				
			

Output : Open the readcontent.txt file to see the tuple.

1 2 3

 

 

get the file size of a file.

check whether a file is closed or not.

Python file program to get the size of a file

In this python file program, we will get the size of 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.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
				
			
Steps to solve the program
  1. Import the os library.
  2. Open the file using os.stat(‘readcontent’) and store the output in the variable.
  3. Get the size of the file using info.st_size.
  4. Print the output.
				
					# Import os library
import os
# Read file
info = os.stat('readcontent.txt')
# Print size of the file
print("Size of the file: ",info.st_size)
				
			

Output:

Size of the file: 193

 

 

count the number of lines in a file.

write a tuple to a file.

Program to count the number of lines in a file

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

				
					Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
				
			
Steps to solve the program
  1. Open the first file by using open(“readcontent.txt”,”r”).
  2. Read the lines in the file using readlines().
  3. Create a count variable and assign its value equal to 0.
  4. Use a for loop to iterate over lines in the file.
  5. After each iteration add 1 to the count variable.
  6. Print the output.
				
					# Open file in read mode
file=open("readcontent.txt","r")
# Read lines
lines= file.readlines()
# Create count variable
count = 0
# Iterate over lines
for line in lines:
# Add 1 to count for each line
    count += 1
# Print output
print("Total number of lines in the file: ",count)
				
			

Output : 

Total number of lines in the file: 8


compare two files.

get the file size of a file.

Python file program to compare two files

In this python file program, we will compare two files with the help of the below-given steps.. Let’s consider we have file1.txt and file2.txt files with the below content.

				
					#file1.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.

#file2.txt
Jay - 3569872214
Yash - 9865472589
Ketan - 8854632255
Rajesh - 7020589634
				
			
Steps to solve the program
				
					# Impoer difflib library
import difflib 
# Open file and read lines
with open('file1.txt') as file_1:
# Open file and read lines
    file_1_text = file_1.readlines() 
with open('file2.txt') as file_2:
    file_2_text = file_2.readlines()
for line in difflib.unified_diff(
        file_1_text, file_2_text, fromfile='file name',
        tofile='file.txt', lineterm=''):
    print(line)
				
			

Output :

--- file1.txt
+++ file2.txt
@@ -1,8 +1,4 @@
-Line1 : This is India.

-Line2 : This is America.

-Line3 : This is Canada.

-Line4 : This is Australia.

-Line5 : This is Africa.

-Line6 : This is Korea.

-Line7 : This is Germany.

-Line8 : This is China.
+Jay - 3569872214

+Yash - 9865472589

+Ketan - 8854632255

+Rajesh - 7020589634

count the number of lines in a file.