In: Computer Science
(python) Write a function that involves two arguments, named changeTheCase(myFile, case),thattakes, 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 lowercase, 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."
def changeTheCase(name,case):
try:
#opening the file
#takes 2 args
#name and mode
# r - read mode
f = open(name, "r")
f1= open("upperCase.txt", "w")
except IOError:
print ("Error: can\'t find file or read data")
for x in f:
#checking if it is upper than converting to upper and writing to
file
if case=="upper":
x=x.upper()
else:
x=x.lower()
f1.write(x)
f.close()
f1.close()
print("Converted Successfully...!!!")
changeTheCase("myfile.txt", "upper")
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me