0

So I have this problem: I want to delete a specific item from a .txt file but when I launch the file It just closes! If I have my code like this It just closes:

nc = input('Name:')
f = open("info.txt", "w")
for line in f: 
  if line != nc: 
    f.write(line)

But when I change my code like this:

nc = input('Name:')
f = open("info.txt", "w")
for line in f: 
  if line != nc: f.write(line)

It works until I get to the point of code when I`m deleting something and then just closes and deletes everything in the .txt file!

Someone, please help me!

Double
  • 23
  • 1
  • 7

1 Answers1

-1

Try changing the open list to "w+". Like this:

f = open("info.txt", "w+")
gtomer
  • 5,643
  • 1
  • 10
  • 21