Write a method/function BIGWORDS() in Python to read contents from a text file CODE.TXT, to count and display the occurrence of those words, which are having 5 or more alphabets. For example: If the content of the file is ME AND MY FRIENDSENSURE SAFETY AND SECURITY OF EVERYONE The output Should be: 5 *
Answers
Answer:
def BIGWORDS(x,y="copy.txt"):
myf=open(x)
myf1=open(y,"w")
list= myf.split(" ")
for i in list:
if len(i)>=5:
myf1.write(i)
myf1.write(" ")
#main
Y=input("enter file name to copy data ")
z= BIGWORDS(CODE.TXT)
In this program there is a assumption that there is a space after each fullstop(.).
Answer:
The below Python code performs the desired function:
def BIGWORDS(temp, call="copy.txt"):
file1 = open(x)
file2 = open(y,"w")
arr = file1.split(" ")
for i in arr:
if len(i)>=5:
file2.write(i)
file2.write(" ")
#Driver Code
temp = input("enter file name to copy data ")
call = BIGWORDS(CODE.TXT)
Explanation:
Let's understand the code above:
- Our task is to create a function name BIGWORDS that reads content from a text file CODE.TXT.
- And then we print count and display the occurrences of those words that have 5 or more alphabets.
- First, we create a temp variable to copy the data of the CODE.TXT so that the original data of the file will not be affected.
- Now look at the definition of the BIGWORDS function, we have passed both values in the function.
- We use the open() function of Python to read the content of the files.
- While reading the data we check each word and if any word is having a length of 5 or more than 5 we write it in our second file and return it.
#SPJ2