Question

Question 2 (10) There are three steps (open, action, close) required for handling files. You need...

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.

Homework Answers

Answer #1

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

Know the answer?
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
Similar Questions
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
I cannot upload the needed text files to this question for your benefit of double checking...
I cannot upload the needed text files to this question for your benefit of double checking your solution, but I need help on this multi part question. Thanks! Write a Python program that will open the ‘steam.csv’ file. Your program should load the data as col- umns corresponding to the variable names located in the first row (You can hard code this – i.e open the file and use the names in the first row as variable names). You should...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a text file data_in.txt with the following information (highlight the piece of text below with numbers and copy it to a text file): 54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,...
I can open the file in the program, but I cannot figure out how to properly...
I can open the file in the program, but I cannot figure out how to properly split the data and assign a grade to the number values in the text file. I keep getting :: ValueError: invalid literal for int() with base 10: 'Roger Jones 75\n' Below are the assignment details: This program processes grades for a small class. It reads an input file named grade_input.txt where each record contains a student’s first name, last name and numeric grade (a...
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold: Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60 More details about the method you need to write are...
1. By convention, how are configuration files separated from regular files? (NOTE: A practical effect of...
1. By convention, how are configuration files separated from regular files? (NOTE: A practical effect of the separation is that they are not displayed by the default version of the ls command)                         a. the prefix "rc" (rc.filename)               c. the extension .cfig                         b. a dot (.) at the beginning                    d. by having the SUID bit set 2. The IP address which is reserved for local loopback (equivalent to "localhost") is:                         a. 255.255.255.0                                   c. 192.168.70.1...
Python Programming Build a python programming that asks the user for a three-letter substring. The program...
Python Programming Build a python programming that asks the user for a three-letter substring. The program should then proceed to read the file strings.txt. Each line in strings.txt will contain a string. Your program should then test to see how many non-overlapping occurrences of the three-letter substring occur in that string and test whether the string is a palindrome. The program should then proceed to create an output file string_stats.txt, which contains the original data on one line and the...
My assignment is listed below. I already have the code complete, but I cannot figure out...
My assignment is listed below. I already have the code complete, but I cannot figure out how to complete this portion: You must create a makefile to compile and build your program. I can't figure out if I need to create a makefile, and if I do, what commands do I use for that? Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber...
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT