Question

In: Computer Science

Using Python. A file exists on the disk named students.txt. The file contains several records, and...

Using Python.

A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student’s name, and (2) the student’s
score for the final exam. Write code that deletes the record containing “John Perz”as the student name.

This the code i have so far but it's not working:

import os

def main():

found=False

search='John Perz'

student_file = open('student.txt','r')

temp_file = open('temp_students.txt','w')

name=student_file.readline()

score=''

while name !='':

score=student_file.readline()

name=name.rstrip('/n')

score=score.rstrip('/n')

if name !=search:

temp_file.write(name+'/n')

temp_file.write(score+'/n')

else:

found=True

name=student_file.readline()

student_file.close()

temp_file.close()

os.rename('temp.text','student.text')

if found:

print("The record was deleted")

else:

print("Record not found")

main()

Solutions

Expert Solution

Short Summary:

Implemented the program as per requirement
Attached source code and sample output


**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

def main():
with open("C:\\Users\\asus\\student.txt") as f:
lines = f.readlines()
for ind, line in enumerate(lines):
if line.startswith("John Perz"):
lines[ind] = ""
with open("C:\\Users\\asus\\student.txt","w") as f:
f.writelines(lines)
main()

Code Screenshot:

Output:

Input file:

OutputFile:


**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

In Python: Assume a file containing a series of integers is named numbers.txt and exists on...
In Python: Assume a file containing a series of integers is named numbers.txt and exists on the computer's Disk. Write a program that reads al the numbers stored in the file and calculates their total. - create your own text file called numbers.txt and put in a set of 20 numbers (each number should be between 1 and 100). - Each number should be on its own line. - do not assume that the file will always have 20 numbers...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Python: Trivia Questionaire Create a trivia application using the provided file which contains 15 questions in...
Python: Trivia Questionaire Create a trivia application using the provided file which contains 15 questions in it. Your application should randomly select how many questions will be presented to the user and it must be a number not greater than the number of questions in the file. Every question that is displayed should be randomly selected from the list and should be non-repetitive; in other words, you should not ask the same question twice. Your application should keep track of...
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
Python, filing and modules Modify the driver to write the statistics to a file named employee-stats.txt...
Python, filing and modules Modify the driver to write the statistics to a file named employee-stats.txt rather than to the console. driver.py code: from employee import Employee employees = [] # Load an employee object for each employee specified in 'employees.txt' into the employees list. employee.txt Jescie Craig VP 95947 Marny Mckenzie Staff 52429 Justin Fuller Manager 72895 Herman Love VP 108204 Hasad Wilkins Staff 96153 Aubrey Eaton Staff 76785 Yvonne Wilkinson Staff 35823 Dane Carlson Manager 91524 Emma Tate...
do not use these: {,},{} Python 3 APPL 276.75 15 A file named asset.txt consists of...
do not use these: {,},{} Python 3 APPL 276.75 15 A file named asset.txt consists of only the three lines above. They describe the unit cost and quantity of shares of Apple stock. Write statement that open the file, read the data and use it to print this exact output: My APPL stock is worth $4,151.25.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
A college records the module registration information of their students in a database named College.mdb,which contains...
A college records the module registration information of their students in a database named College.mdb,which contains three tables,STUDENT,MODULE andMODULE_REG. A student may enroll in the same module as many times as they like. However, they can only enroll each module at most once in each semester. The design of the three tables are shown below: Table STUDENT Field Name Data Type Note SID Short Text primary key FirstName Short Text LastName Short Text Contact Short Text Gender Short Text “M”...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them by your own. A constructor that creates a table with the following: a list of data. ip address a integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the ip address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT