+ 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
9 ответов
+ 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)
+ 2
a=open(‘filename.txt’, ‘w+’)
a.write(the names goes here)
Line=a.read()
0
That depends on how the names are written to the file. Provide examples and your attempt
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.
0
Okay, so lets see your attempt at that
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)
0
We have to write in the file the list before this
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.
0
This is the question