Question

In: Computer Science

Send this as a Python file. Note: this is an example where you have the original...

Send this as a Python file. Note: this is an example where you have the original file, and are writing to a temp file with the new information. Then, you remove the original file and rename the temp file to the original file name. Don't forget the import os statement.A file exist on the disk named students.txt The file contains several records and each record contains 2 fields :1. The student's name and 2: the student's score for final exam. Write a code that changes Julie Milan's score to 100.

Here is text

Mark Lozoya

85

Hector Guevara

98

Jeff Gohrick

79

Julio Gonzalez

90

Dwayne Hudson

81

Julie Milan

92

Johnny Lechuga

90

Mark Martinez

91

Mary Robinson

100

Norma Batista

69

Solutions

Expert Solution

Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:
students.txt before the code is executed:


Console output:


students.txt after the code finishes execution:

Below is the code to copy:

#CODE STARTS HERE----------------
import os
#importing name and score for updation
name = input("Enter the name of student:")
score = input("Enter the score: ")

f_temp = open("temp.txt", 'a')#creating and opening a temp file

with open("students.txt",'r') as f:#open students.txt
   key = 0 #Key becomes 1 if student name is found
   for line in f: #loop line by line
      if key == 1: #if name found, make changes to score
         f_temp.write(score+"\n") #write updated score
         key=2 #change key status once changes are made
         print("Score updated")
         continue
      f_temp.write(line) #Just update from students to temp
      if name.lower() in line.lower(): #Check if name is found
         key = 1 #change key value
   if key == 0: #key will be 2 if any match happens
      print("Name not found")
f_temp.close() #close temp file

if os.path.exists("students.txt"): #check and remove students.txt
   os.remove("students.txt")
else:
   print("File delete Error")

if os.path.exists("temp.txt"): #Check and rename temp.txt
   os.rename("temp.txt", "students.txt")
else:
   print("File rename Error")
#CODE ENDS HERE-----------------

Related Solutions

Provide an original example of a transaction and then identify the type receivable—accounts receivable, a note...
Provide an original example of a transaction and then identify the type receivable—accounts receivable, a note receivable, or other receivables.
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a...
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a...
Can you send the excel file if I provide an email address? This is a question...
Can you send the excel file if I provide an email address? This is a question on managerial decision making with excel spreadsheet. A step wise how to solve this will be appreciated for homework and not any exam. A model in excel will really help. John’s Truck John owns John’s Truck Rental, which is currently for sale for $650,000. He would like you to develop a five-year spreadsheet model to assist buyers in their evaluation of the company. The...
c# Create a console application that protects an XML file, such as the following example. Note...
c# Create a console application that protects an XML file, such as the following example. Note that the customer's credit card number and password are currently stored in clear text. The credit card must be encrypted so that it can be decrypted and used later, and the password must be salted and hashed: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <name>Bob Smith</name> <creditcard>1234-5678-9012-3456</creditcard> <password>Pa$$w0rd</password> </customer> </customers>
Provide an original example where you perform a hypothesis test using the one-mean z-test. Provide within...
Provide an original example where you perform a hypothesis test using the one-mean z-test. Provide within this part the following items: State the null hypothesis. State the alternative hypothesis test. State the chosen alpha level of significance. Use the critical value approach to determine whether to reject or fail to reject the null hypothesis. Use the p-value approach to determine whether to reject or fail to reject the null hypothesis. Discuss how a Type I or Type II error could...
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
Write a program in Python where you can swap only two consecutive elements. You have to...
Write a program in Python where you can swap only two consecutive elements. You have to show all steps to convert a string into another string (both strings will be anagrams of each other). E.g. GUM to MUG GUM GMU MGU MUG
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT