Question

In: Computer Science

Language: Python 3 (Please structure answer as basic as possible) Write a function that involves two...

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

Solutions

Expert Solution

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:


Related Solutions

(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...
Please Write-In Python Language (Topic: Word frequencies) Method/Function: List<Token> tokenize(TextFilePath) Write a method/function that reads in...
Please Write-In Python Language (Topic: Word frequencies) Method/Function: List<Token> tokenize(TextFilePath) Write a method/function that reads in a text file and returns a list of the tokens in that file. For the purposes of this project, a token is a sequence of alphanumeric characters, independent of capitalization (so Apple, apple, aPpLe are the same token). You are allowed to use regular expressions if you wish to (and you can use some regexp engine, no need to write it from scratch), but...
Please use Python 3 3). Write a function that writes a series of random numbers to...
Please use Python 3 3). Write a function that writes a series of random numbers to a text file named ‘random_number.txt’. Each random number should be in the range of 1 through 500. The function should let the user specify how many random numbers the file will hold. Then write another function that reads the random numbers from the ‘random_number.txt’ file, displays the numbers, and then displays the total of the numbers and the number of random numbers read from...
(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...
Please write a basic function using Python. Please comment all steps. Thank you! Experimentally determined molecular...
Please write a basic function using Python. Please comment all steps. Thank you! Experimentally determined molecular structures are stored in the Protein Data Bank. Protein Data Bank format is a standard for files containing atomic coordinates which are stored in the “ATOM” record. Write a Python function to extract x coordinate of each atom for a given PDB file. Test your function with the provided “1a3d.pdb” file as the example. Also, give a good thought what would be the proper...
Please use Java language to write two efficient functions: Write an efficient function that compute the...
Please use Java language to write two efficient functions: Write an efficient function that compute the intersections of two sorted arrays of integers Write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Please use Java language to write an efficient function that compute the intersection of two arrays...
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
Answer as soon as possible!!! Code must be done with Python Develop a basic File Transfer...
Answer as soon as possible!!! Code must be done with Python Develop a basic File Transfer Protocol (FTP) application that transmits files between a client and a server using Python. Your programs should implement four FTP commands: (1) change directory (cd), (2) list directory content (ls), (3) copy a file from client to a server (put) and (4) copy a file from a server to a client (get). Implement two versions of the program: one that uses stock TCP for...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and use a function named highest() that takes three inputs and returns the highest number. After you have got it working, try calling the function with inputs ‘hat’, ‘cat’, ‘rat’.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT