Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Week-7 Python Lab Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Week-7 Python Lab Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Week- 7

1. Write a Python code to merge two given file contents into a third file.
First file: file1.txt
CMR TECHNICAL CAMPAS
OUR VISION:
To Impart quality education in serene atmosphere thus strive for excellence in Technology and Research.

Second file: file2.txt


OUR MISSION:
To Create state of art facilities for effective Teaching- Learning Process.
Pursue and Disseminate Knowledge based research to meet the needs of Industry & Society.
Infuse Professional, Ethical and Societal values among Learning Community.

Merge.py
# Creating a list of filenames
filenames = ['file1.txt', 'file2.txt']

# Open file3 in write mode


with open('file3.txt', 'w') as outfile:

# Iterate through list


for names in filenames:

# Open each file in read mode


with open(names) as infile:

# read the data from file1 and file2 and write it in file3
outfile.write(infile.read())

# Add '\n' to enter data of file2


# from next line
outfile.write("\n")

mergefile = open("file3.txt")

# Reading from file


print(mergefile.read())

mergefile.close()
2. Write a Python code to open a given file and construct a function to check for given words
present init and display on found.

Note : create a text file (i.e.. sample.txt) in which we want to count the words in Python

Example:
file1.txt
CMR TECHNICAL CAMPAS
OUR VISION:
To Impart quality education in serene atmosphere thus strive for excellence in Technology and Research.

Search.py
#To Construct a function to Search a word in a file
def search_word(file_path, word):
with open(file_path, 'r') as file:
# read all content of a file
content = file.read()
# check if string present in a file
if word in content:
print('The word {0} exist in a file'.format(word))
else:
print('The word {0} does not exist in a file'.format(word))

#To Enter a word for check given words present in it or not


word=input("Enter a word for searching in file :")
search_word(r'file1.txt', word)
3. Write a Python code to Read text from a text file, find the word with most number of occurrences

Note : create a text file (i.e.. sample.txt) in which we want to count the words in Python

Example:
Sample.txt
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry

Count_word.py

fname = input("Enter file name: ")


word=input("Enter word to be searched:")
k = 0

with open(fname, 'r') as f:


for line in f:
words = line.split()
for i in words:
if(i==word):
k=k+1
print("Occurrences of the word {0} is :".format(word))
print(k)
4. Write a function that reads a file file1 and displays the number of words, number of
vowels, blankspaces, lower case letters and uppercase letters.
# program to displays the number of words, number of vowels,
# blankspaces,lower case letters and uppercase letters
def analyze_file(file_name):
try:
# Opening the file in read mode
with open(file_name, 'r') as file:
#Read the file Content
content=file.read()
#count the number of words
words=content.split()
word_count=len(words)
#count the number of vowels
vowels="aeiou"
vowel_count=sum(content.lower().count(vowel) for vowel in vowels)
#count the number of blank spaces
blank_space_count=content.count(' ')
# count the number of lowercase letters
lowercase_count = 0
uppercase_count = 0
for i in content:
if i.isupper() == True:
uppercase_count = uppercase_count + 1
elif i.islower() == True:
lowercase_count = lowercase_count + 1
else:
None
# Print the desired output on the console.
print(f"Number of words : {word_count}")
print(f"Number of vowels : {vowel_count}")
print(f"Number of Blankspaces: {blank_space_count}")
print(f"Number of Lowercase letters : {lowercase_count}")
print(f"Number of Uppercase letters : {uppercase_count}")
except FileNotFoundError:
print("File not found.")
# Calling the function counting which gives the desired output
analyze_file('file1.txt')

You might also like