Question

Write a function called backwards which takes two parameters. The first is a file object that has already been opened in read

python program please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. As asked in the question that to implement a Python function named as backwards where it is required to read a file line by line and write each line in a file should have the words in reversed order.

2. As per requirement, a complete program has been written with the function and few lines are written for testing in a file named as backwards_demo.py and the file is saved at D:/.

3. Please find below backwards_demo.py file screenshot including comments:-

B backwards.py #Defining function backwards which is taking essayfile object of File type and the filename in which data needessayfile = open(essay.txt,r) 000 ON #calling backwards function with the essayfile file type object and the file name ou

4. Please find below backwards_demo.py file in text form including comments:-

#Defining function backwards which is taking essayfile object of File type and the filename in which data need to be written
def backwards(essayfile, filename):

   #Creating a writeFile object with the filename
   writeFile = open(filename, "w")
  
   #Iterating over each line of the essayfile
   for line in essayfile:
      
       #Splitting each word of the line and storing it into the wordsList
       wordsList=line.split()
      
       #Then iterating over each word of the wordsList from the end
       #as the index is given -1 i.e. read from the end of the file
       for word in wordsList[::-1]:
           writeFile.write(word+' ') #writing word to the file

       #Write a new line into the file after reading each word of the line
       writeFile.write('\n')
  
   #Closing writeFile object
   writeFile.close()

#End of the backwards function

#Below three lines are written to test the functionality of the backwards function
#Below line is creating a essayfile object to read the data from the file named as essay.txt
essayfile = open('essay.txt',"r")
          
#Calling backwards function with the essayfile file type object and the file name output.txt in which
#the output should be written
backwards(essayfile, 'output.txt')

#Closing the essayFile object
essayfile.close()

5. Please note that the last three lines are written in the above program is just for testing purpose only where the input and output file names are specified with the backwards function call. Input file name is essay.txt and output file name is output.txt.

6. Please fine below contents of essay.txt given as input:-

essay but This is a Test! 2 It works perfectly fine. 3 The life is beautiful. 4 It gives us lot of opportunity. 5 I am greatf

7. Please fine below contents of output.txt after executing above program:-

Boutput.txt x 1 Test! a is This fine. perfectly works It 3 beautiful. is life The 4 opportunity. of lot us gives It 5 life. b

It is clearly visible from the above two screenshots that each line of essay.txt has been reversed with the words. This is what the we are expecting from our program.

8. Please find below screenshot showing how the above program is executed at D:/:-

D:\>python backwards_demo .py D:\>

9. Important part of the code is marked in bold.

10. Though the complete program is implemented as per instructions mentioned in the question. Still if you face any difficulty while executing this code or any part of the explanation, you can feel free to post your query in comments section. I will reply as soon as possible with utmost importance.

11. Please rate positive if you like my solution.

Have a nice day.

Add a comment
Know the answer?
Add Answer to:
python program please Write a function called backwards which takes two parameters. The first is a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • Python Write a function that takes in two numbers as parameters. The function will then return...

    Python Write a function that takes in two numbers as parameters. The function will then return the value of the first parameters squared by the second parameter. Print the value of the return statement. Take a screenshot of both your code and your properly executed code after you have run it. Submit the screenshots here along with your .txt file.

  • Please write this code in python programming and provide a screen short of the code. Reading:...

    Please write this code in python programming and provide a screen short of the code. Reading: P4E 7; Tut 7.2, 7.2.1 Upload an original Python script that satisfies the following criteria: It has at least two functions Function 1: This function has one parameter, a string containing the path to an existing text file that will be read by your function Using a with statement, the function opens the file indicated by the parameter for reading The function counts the...

  • 2. Write a function file_copy() that takes two string parameters: in file and out_file and copies...

    2. Write a function file_copy() that takes two string parameters: in file and out_file and copies the contents of in_file into out file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output. 1 >>> file_copy( created equal.txt', 'copy.txt) 2 >>> open fopen ( copy.txt') 3 >>> equal-f = open( 'created-equal . txt') 4 >>equal_f.read () 5 'We hold these truths to be self-evident, Inthat all men are created equalIn' 3. Write:...

  • python Define a function called print_values which takes a dictionary object as a parameter. The function...

    python Define a function called print_values which takes a dictionary object as a parameter. The function should print all values in the dictionary. However, the order is based on the sorted keys in the dictionary. For example, if we have the following dictionary: {'b':36, 'a':12, 'c':24} The output is: 12 36 24 A faulty solution has been provided below. Identify the fault and submit a corrected version of this code. def print_values(dict1): for k in list(dict1.keys()).sort(): print(dict1[k], end=" ") For...

  • Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and...

    Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and divides the first parameter by the second. Your divide_two_numbers function must check the second parameter before dividing; if it is zero, raise an ZeroDivisionError exception which includes the string "second parameter was zero!" as a parameter instead of processing the division. Your main function must call your divide_two_numbers function at least twice; one call must use two normal parameters to verify normal operation, the...

  • Using python 3.6.0 Write a function, called cubic, which takes in two parameters, say x and...

    Using python 3.6.0 Write a function, called cubic, which takes in two parameters, say x and y and computes the following formula: x3 + x + y and returns the computed value. Sample output 1: Enter x: 2 Enter y: 1 The value of the function is : 11 Sample output 2: Enter x: 1 Enter y: 3 The value of the function is : 5

  • Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two...

    Python 3.6 Question 12 (20 points) Write a function named wordLengths. The function wordLengths takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file exists when wordLengths is called; wordLengths must create the file outFile. The input file contains only letters and white space For each line of the input file, wordLengths should identify the length of the longest word...

  • 5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename),...

    5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename), starting line number i.e. m) and ending line number (i.e. n) as a parameter. The function then returns the contents from line number m to n (m <n). If m < 1 then start from line 1. Similarly, if n > number of lines the file, then stop at the last line in the file. Sample Input: readlinesmton("data.txt",5,7) Sample Input: readlinesmton("data.txt", 0,10)

  • Write a complete program that uses the functions listed below. Except for the printOdd function, main...

    Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT