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 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...
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...
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"...
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...
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...
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’.
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Programming language is in python 3 For this project, you will import the json module. Write...
Programming language is in python 3 For this project, you will import the json module. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a...
Programming language is python 3 For this project, you will import the json module. Write a...
Programming language is python 3 For this project, you will import the json module. Write a class named NeighborhoodPets that has methods for adding a pet, deleting a pet, searching for the owner of a pet, saving data to a JSON file, loading data from a JSON file, and getting a set of all pet species. It will only be loading JSON files that it has previously created, so the internal organization of the data is up to you. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT