Question 2 (10)
There are three steps (open, action, close) required for handling
files. You need to write a Python program what executes each of
these three steps. Your program must include:
Extensive comments to ensure explanation of your code (1).
Open a file (file name must include your student number, thus
filexxxxxxxx.txt). (1).
Write the details of the five students (from Question 1 above) to
a file (3).
Close the file. (1)
Then open the file again (1).
Write the contents of the file to display on the screen (3).
Please make thre answer simple for a beginner, Thank you.
Hi. I have answered previous version of this question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
EDIT: screenshots attached.
#code
# Student class
class Student:
# constructor initializing student fields to
default values
def __init__(self):
self.__name =
'' # string name, ex: John
self.__number = 0
# int number, ex: 100
self.__contact =
'' # string contact number, ex:
123-457-9991
self.__id =
'' # string id number, ex: ABC112
# method to read data from user and fill the
fields of this Student
def populate(self):
self.__name =
input('Enter name: ')
self.__number =
int(input('Enter student number: '))
self.__contact =
input('Enter contact number: ')
self.__id =
input('Enter ID number: ')
# method to display student's data to
console
def display(self):
print('Name:', self.__name)
print('Student
number:', self.__number)
print('Contact
number:', self.__contact)
print('ID
number:', self.__id)
#returns a string containing student
details
def __str__(self):
text = "Name:
" + self.__name + ", Student number: " +
str(self.__number) + ", Contact number:" \
+ self.__contact + ", ID number: " +
self.__id
return
text
# end of Student class
# main method
def main():
# creating a list to store details of 5
students
studList = []
# looping for 5 times
for i in
range(5):
# creating a Student
instance
studentObj =
Student()
# populating
student's fields
studentObj.populate()
# adding to
list
studList.append(studentObj)
print() #line
break
#replace 0001 with your student number
filename='file0001.txt'
#opening file in write mode (open)
file=open(filename,'w')
#looping through each student in list
for stud
in studList:
# writing student
data to the file (__str__() method will be invoked) (action)
file.write(str(stud)+'\n')
#closing file, saving changes (close)
file.close()
#opening file in read mode (open)
file=open(filename,'r')
print('File contents:')
#displaying file contents (action)
print(file.read())
#closing file (close)
file.close()
main()
#output
Enter name: Olive
Enter student number: 101
Enter contact number: 1234567890
Enter ID number: A1200
Enter name: John
Enter student number: 102
Enter contact number: 1234567891
Enter ID number: A1201
Enter name: Kevin
Enter student number: 103
Enter contact number: 1234567892
Enter ID number: A1202
Enter name: Jane
Enter student number: 201
Enter contact number: 1112223331
Enter ID number: C10101
Enter name: Cathy
Enter student number: 202
Enter contact number: 1112223332
Enter ID number: C10102
File contents:
Name: Olive, Student number: 101, Contact number:1234567890, ID number: A1200
Name: John, Student number: 102, Contact number:1234567891, ID number: A1201
Name: Kevin, Student number: 103, Contact number:1234567892, ID number: A1202
Name: Jane, Student number: 201, Contact number:1112223331, ID number: C10101
Name: Cathy, Student number: 202, Contact number:1112223332, ID number: C10102
Code screenshot
Output screenshot
Get Answers For Free
Most questions answered within 1 hours.