Question

In: Computer Science

There are three steps (open, action, close) required for handling files. You need to write a...

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).

Solutions

Expert Solution

SOURCE CODE:

FileName=input("Enter the file name:") #reading filename from user
file=open(FileName,'w') #opening file in write mode
n=int(input("Enter Number of students: ")) #reading Number of students from user
Name=str()
IDNo=int()
Age=int() #initializing variable with default values
Grade=str()
temp_str="" #temp_str for file concatenation
print("Enter student Details to write into file:\n")
for i in range(n):
Name=input("Enter Name: ") #reading Name from user
IDNo=int(input("Enter IDNo: ")) #reading IDNo from user
Age=int(input("Enter Age: ")) #reading Age from user
Grade=input("Enter Grade: ") #reading Grade from user
  
#concatenating all variables data which is taken from user
temp_str=str(IDNo)+"\t"+Name+"\t"+str(Age)+"\t"+Grade+"\n"   
file.write(temp_str) #writing concatenated string to file
file.close() #closing the file
print("Details of the students writed successfully")

print("\n")
file=open(FileName,'r') #Againing opening the file in readmode
for record in file: #iterating over each line of file
print(record) #printing the record to console

file.close() #Closing the file
  
CODE SCREENSHOT:

CONSOLE OUTPUT

file1234.txt


Related Solutions

Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the following three methods in the class RandomFile: ​public static void randomBinaryFile(String fileName, int length) ​public static void randomNumberFile(String fileName, intlength) ​public static void randomCharFile(String fileName, int length) The parameter “fileName” specifies the file name, and “length” specifies the length of the sequence in the file. ● randomBinaryFile will generate a file containing a sequence of 0 or 1 of the specified length; ● randomNumberFile...
Programming Steps: (In Java) (Please screenshot your output) A. Files required or needed to be created:...
Programming Steps: (In Java) (Please screenshot your output) A. Files required or needed to be created:    1. Artist.java (Given Below)    2. p7artists.java (Input file to read from)    3. out1.txt (Output file to write to) B. Java programs needed to writeand create:    1. MyArtistList.java:    - Contains the following:        1. list - public, Arraylist of Artist.        This list will contain all entries from "p7artists.txt"        2. Constructor:        A constructor that accepts one...
write a note on cubic close packing and three types 1D close packing 2D close packing...
write a note on cubic close packing and three types 1D close packing 2D close packing and 3D close packing include diagrams thanks
Purpose This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and...
Purpose This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(), perror(), and command line arguments. Program Write a C++ program that will allow you to add messages to a file that has NO permissions for any user. A Unix system has many files that have sensitive information in them. Permissions help keep these files secure. Some files can be publicly read, but can not be altered by a regular user (ex.: /etc/passwd). Other...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
a. You have to write the steps that we need to insert a new node as...
a. You have to write the steps that we need to insert a new node as the head of an existing linked list. b.You have to write the code in c++ programming language of the function that we need to insert a new node in the end of an existing node. c.Suppose that the below main function is executed correctly and all the functions that are invoked are imported from functions.h file. Explain. int main(){ Node *head=NULL; insertEnd(&head,"John");//inserts a new...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep...
Write a GUI-based program that allows the user to open, edit, and save text files. The...
Write a GUI-based program that allows the user to open, edit, and save text files. The GUI should include a labeled entry field for the filename and multi-line text widget for the text of the file. The user should be able to scroll through the text by manipulating a vertical scrollbar. Include command buttons labeled Open, Save, and New that allow the user to open, save and create new files. The New command should then clear the text widget and...
You are the Emergency Manager responsible for handling this emergency. List the steps you would take...
You are the Emergency Manager responsible for handling this emergency. List the steps you would take to respond to the initial calls being received. How would you assess initial damage and the areas of the city that are in most need? How would you address the problems that are likely to last for multiple days, e.g., a collapsed freeway with trapped citizens? Provide illustrative examples to support your position.
Action steps to meet goals. identify what are some of the things leaders need to stop...
Action steps to meet goals. identify what are some of the things leaders need to stop doing, start doing, and keep doing. These short term actions will contribute directly to your long term goals. what potential barriers can you think of that might hinder pursuing your goals? how so you overcome these barriers? what are some strengths that you can use to help achieve your goals?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT