Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Describe what the python code/block do each line there is a comment For each Que

ID: 3724795 • Letter: D

Question

Describe what the python code/block do each line there is a comment

For each Question, provide a series of detailed comments below describing what the code does. Use as much space as you need. The first question appears in the main function. There are 30 questions that require you to provide detailed comments explaining what the line or block of code does import random import os # Question No. 8 class File: # Question No. 9 def-init--(self, fileName): self.name = self.handle fileName = open(self.name, 'a') # Question No. 10 def addLines(self,text): self.handle.write(text + ' # Question No. 11 def addRandomText(self, numberOfLines): fori in range(numberOfLines): self.handle.write(Som Random Text n) # Question No. 12 def deleteFile(self: os.remove(self.name) # Question No. 13 def displayFileContents(self): for line in self.name: print(line) # Question No. 14 def closeFile(self): self.handle.closeO

Explanation / Answer


Hi,

For the each the comments, I have included detailed description. I hope it helps you to understand the code better.
Let me know if you need any help.


Q8: class File
Description: Define a class with name File that is supposed to support method to read and write to given file.


Q9: def __init__(self, fileName)
Description: Here, a constructor method that accepts an argument "fileName" is defined. The name of the file is stored in an attribute "name" and another attribute "handle" is initialized after the file is opened in append mode.
The attribute "handle" is later used for all methods which read or write to the file.

In python, the constructor is defined as __init__(self) or with arguments as __init__(self, fileName).
An object of this class is created as myNewFile = File()

Here, "myNewFile" refers to the object

Q10: def addLines(self, text)
Description: A method - addLines with argument "text" is defined. This method writes the value of the argument to the file using "handle" attribute that was initialized in the constructor.

The "write" method is the method provided by File module that is pre-packaged as Python library.

Q11: def addRandomText(self, numOfLines)
Description: This method writes the string "Som Random Text" to the file in different lines. The number of lines to be written as passed as an argument to this method. The "for" loop is used to iterate and write to the file.

Q12: def deleteFile(self)
Description: In this method, we use the "os" module's remove method to delete the file. The name of the file is referred by this class's attribute "name"

Q13: def displayFileContents(self)
Description: This method refers to the attribute "name" and the prints the name of the file in each line.

Please note: From the name of this method, it is supposed to read the contents of the file but the code rather prints the file name. To read the actual contents of the file, the read() method should have been invoked. I'm not sure if it was deliberately skipped or it was overlooked by the instructor.

for line in self.handle.read()
print(line)


Q14: def closeFile(self)
Description: In this method, the attribute "handle" which refer to the file handle that was initialized in the constructor is closed. After this class, the file will be completely updated and any subsequent read or write methods will result in errors.