Question

In: Computer Science

(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...

(In python)

4. 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.”

Solutions

Expert Solution

In this program, we have to convert the contents of given file to either lowercase or uppercase.

To do that, first check the value of case parameter. If case is "upper" , then open myFile in read mode, read all the contents of the file using read() function. Convert the contents to upper case using upper() function, and finally write the converted content to upperCase.txt

Otherwise, if case is "lower", then follow the same process as above, but instead of converting the contents to upper case using upper() , convert the contents to lowercase using lower() function. And the converted contents are to written to 'lowerCase.txt'

program:

def changeTheCase(myFile, case):
if case=="upper":
f = open(myFile,'r')
data = f.read()
data = data.upper()
f2 = open('upperCase.txt','w')
f2.write(data)
f.close()
f2.close()
return "Converted file to upper case."
if case=="lower":
f = open(myFile,'r')
data = f.read()
data = data.lower()
f2 = open('lowerCase.txt','w')
f2.write(data)
f.close()
f2.close()
return "Converted file to lower case."

return "Invalid parameter"

I will use a sample file named input.txt to test this function. This is input.txt file:

sample function calls:

print(changeTheCase('input.txt','lower'))
print(changeTheCase('input.txt','upper'))
print(changeTheCase('input.txt','yibbe'))

outputs:

lowerCase.txt:

upperCase.txt:


Related Solutions

(python) Write a function that involves two arguments, named changeTheCase(myFile, case),thattakes, as arguments, the name of...
(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...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a boolean function named isMember that takes two arguments: an array of type char and...
Write a boolean function named isMember that takes two arguments: an array of type char and a value. It should return true if the value is found in the array, or false if the value is not found in the array. PLEASE WRITE FULL PROGRAM IN C++ AND USE RECURSION AND DO NOT USE LOOPS
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality...
a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality checking function that takes two values and returns a value of type bool, a list of values that are to be separated, and a list of separators values. This function will split the second list into a list of lists. If the checking function indicates that an element of the first list (the second argument) is an element of the second list (the third...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If  
. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If   value1   and   value2   are   either   integers   or   strings   containing   only   digits,   cast   value1   and   value2   to   integers   and   compute   their   average.       If   their   average   is   greater   than   50,   return   the   string   “Above   50”   and   if   the   average   is   less   than   50,   return   the   string   “Below   50”.       If   the   average   is   equal   to   50,   return   the   string   “Equal   to   50”,   and   if   value1   or  ...
PYTHON Basic Feature[5pts] Write a function which takes two arguments—a string (str1) and a position index...
PYTHON Basic Feature[5pts] Write a function which takes two arguments—a string (str1) and a position index (n). The function will: 1.Remove the n-th characterof str1 (the initial character is the 0-th character) 2.Swap the order of the substring in front of the removed character and the substring afterwards, and concatenate them as a new string 3.Return the converted (str2) back to the function caller For example, myfunc(“abc1xyz”, 3) returns “xyzabc”. Additional Features [6 pts] Expand the function by adding more...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT