0% found this document useful (0 votes)
5 views4 pages

Python File Operations and Analysis

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)
5 views4 pages

Python File Operations and Analysis

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

Week- 7

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

Second file: [Link]


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.

[Link]
# Creating a list of filenames
filenames = ['[Link]', '[Link]']

# Open file3 in write mode


with open('[Link]', '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
[Link]([Link]())

# Add '\n' to enter data of file2


# from next line
[Link]("\n")

mergefile = open("[Link]")

# Reading from file


print([Link]())

[Link]()
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.. [Link]) in which we want to count the words in Python

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

[Link]
#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 = [Link]()
# 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'[Link]', 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.. [Link]) in which we want to count the words in Python

Example:
[Link]
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 = [Link]()
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=[Link]()
#count the number of words
words=[Link]()
word_count=len(words)
#count the number of vowels
vowels="aeiou"
vowel_count=sum([Link]().count(vowel) for vowel in vowels)
#count the number of blank spaces
blank_space_count=[Link](' ')
# count the number of lowercase letters
lowercase_count = 0
uppercase_count = 0
for i in content:
if [Link]() == True:
uppercase_count = uppercase_count + 1
elif [Link]() == 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('[Link]')

You might also like