In: Computer Science
Language: Python 3 (Please structure answer as basic as possible)
Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function will open the file, convert all characters on each line to lower case, write each line to a new file, named “lowerCase.txt”, and return the string “Converted file to lower case.” If case is any other value, the function returns the string “Invalid parameter.”
For example: >>> changeTheCase(“hello.txt”, “upper”) should open the file named hello.txt, convert each character in each line to upper case, write each converted line to a new file named upperCase.txt, close both files, and return the string “Converted file to upper case.”
As another example: >>> changeTheCase(“hello.txt”, “lower”) should open the file named hello.txt, convert each character in each line to lower case, write each converted line to a new file named lowerCase.txt, close both files, and return the string “Converted file to lower case.”
As a final example: >>> changeTheCase(“hello.txt”, “yibbie”) should return the string “Invalid parameter.”
Code:
import os
#functio to change the case
def changeTheCase(myFile,case):
#opening the file
with open(myFile,'r') as x:
#if required case is uppercase
if(case=="upper"):
#convert content to uppercase
y=x.read().upper()
#if required case is lowercase
elif(case=="lower"):
#convert content to lowercase
y=x.read().lower()
#write the content in a upperCase file
if(case=="upper"):
with open('upperCase.txt', 'a') as out:
out.write(y)
#return the string
return("Converted file to Upper case.")
#write the content in a lowerCase file
elif(case=="lower"):
with open('lowerCase.txt', 'a') as out:
out.write(y)
#return the string
return("Converted file to lower case.")
#if any other thing found expect upper or lower
else:
#return the string
return("Invalid parameter")
#calling the function
f=changeTheCase('hello.txt','lower')
#printng the result
print(f)
Output:
case 1:
Case 2:
Case 3:
Above code is completely based upon the requirements mentioned in the case study.
(I) In the function changeTheCase first of all the file is opened in read more and the case is checked for upper or lower. If found the case is upper then by using the read function and at the same time it is converted to uppercase by calling the function upper(). The result is stored in a variable named y.
(ii) If the required case is lower then like the above the content is read from the file and simultaneously converted into lowercase by calling the method lower().
(iii) Now its time to append the content in their respective files. To do that again the case is checked for lowercase and uppercase. If found upper then a new file named upperCase is opened in append mode and the content y is inserted into the file upperCase by using the method write. After successful insertion a string is written stated as converted file to uppercase.
(iv) For lowercase the case is checked and the content y is written in the file lowerCase and after that a string message is returned from the function.
(v) Other than value of case lower and upper a string message with invalid parameter is returned. From the main function the function is called with the file named hello.txt and case lower. You can see the output in the command prompt. Also the files hello, upperCase and lowerCase is showed to you for different test cases in the output.
Screenshot of the code: