File handling in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

File handling in python

I want to input a name that is already in a file and when it is inputed it deletes the name from the file

29th Jun 2022, 7:08 PM
Uwalaka Ugochukwu
9 Answers
+ 2
You may want to brush up on your syntax. name = input() with open(<file>) as names: name_list = names.readlines() if name in name_list: index = 0 for n in range(len(name_list)): if name_list[index] == name: name_list.pop(index) with open(<file>, w) as file: file.writelines(name_list)
29th Jun 2022, 8:45 PM
Slick
Slick - avatar
+ 2
a=open(‘filename.txt’, ‘w+’) a.write(the names goes here) Line=a.read()
30th Jun 2022, 2:46 AM
Vaibhav
Vaibhav - avatar
0
That depends on how the names are written to the file. Provide examples and your attempt
29th Jun 2022, 8:23 PM
Slick
Slick - avatar
0
Create a file with a list of patients awaiting organ donation, write a program where i can enter a patient's name and if the patient exists delete the patient's name from the file but if the patient doesn't exist add it to the file.
29th Jun 2022, 8:26 PM
Uwalaka Ugochukwu
0
Okay, so lets see your attempt at that
29th Jun 2022, 8:27 PM
Slick
Slick - avatar
0
Name=input() X=open(‘txt’, ‘w+’) X.write(the names goes here) Line=x.read() For lines in line: If line == name: Line.remove() Else: X.write(name)
29th Jun 2022, 8:39 PM
Uwalaka Ugochukwu
0
We have to write in the file the list before this
29th Jun 2022, 8:57 PM
Uwalaka Ugochukwu
0
Create a file with a list of patients awaiting organ donation, write a program where i can enter a patient's name and if the patient exists delete the patient's name from the file but if the patient doesn't exist add it to the file.
30th Jun 2022, 2:50 AM
Uwalaka Ugochukwu
0
This is the question
30th Jun 2022, 2:50 AM
Uwalaka Ugochukwu