Week-7 Python Lab Programs
Week-7 Python Lab Programs
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.
Merge.py
# Creating a list of filenames
filenames = ['file1.txt', 'file2.txt']
# read the data from file1 and file2 and write it in file3
outfile.write(infile.read())
mergefile = open("file3.txt")
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))
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