-1

I'm trying to delete from a file.

This is what I am trying to do.

1.importing Easygui

2.Put three modes plus quitting

  1. 3 modes are (see, add, delete)

Everything is working except deleting part. this is what I have

    import easygui

    filea = open('GroceryList.txt', 'a')
    fr = open('GroceryList.txt', 'r')
    filer = open('GroceryList.txt', 'r')

    runinng = True
    while runinng:
      a = easygui.choicebox('Do you want to add, delete, or see your list?',
                    choices = ['add', 'delete', 'see', 'quit'])
     if a == 'add':
       ad = easygui.enterbox('What do you want to add')
       filea.write(ad)
       filea.write('\n')
    filea.close()
    filea = open('GroceryList.txt', 'a')


    elif a == 'delete':
       rn = easygui.enterbox('What are you going to delete?')
       rl = fr.readlines()
       for lines in fr:
         if rn in lines:
           line.split()
         fr.close()
         fr = open('GroceryList.txt', 'r')


   elif a == 'see':
     s = filer.readlines()
     easygui.msgbox(s)
   filer.close()
   filer = open('GroceryList.txt', 'r')

   elif a == 'quit':
    runinng = False
  filea.close()
  fr.close()
  filer.close()

The part that is not working is:

    elif a == 'delete':
rn = easygui.enterbox('What are you going to delete?')
rl = fr.readlines()
if rn in fr:
  line.remove()
fr.close()
fr = open('GroceryList.txt', 'r')
  • You need to open a *new* file with write access (`w`). Loop reading each line from the original and write the line if it is not the one to delete. Close both files then rename the new file to be the same as the old. A bit of a pain, but that's what happens if you use a text file instead of a database. – cdarke Aug 10 '18 at 12:35
  • A line is best deleted from a file this way: 1 read all lines from file into a list, 2 delete one line from the list, 3 open the file again, this time for writing, and write the remaining lines into it – zvone Aug 10 '18 at 12:36
  • Thanks for the advice, but... I don't get it. Can you show me a script? – UrDistraction Aug 10 '18 at 12:43

2 Answers2

0

As others mentioned in the comments just providing a small script to clear out the doubts you may still have

fr = open('GroceryList.txt', 'r')
rl = fr.readlines()
new_list_to_keep_rows=[]
for row in rl:
    #add rows to keep
    new_list_to_keep_rows.append(row) 

new_file = open('GroceryList_new.txt', 'w')# open file in write mode
for item in new_list_to_keep_rows:
    new_file.write("%s\n" % item)

rename the file when you are sure you have everything.

mad_
  • 8,121
  • 2
  • 25
  • 40
0

I noticed one thing in your code:

import easygui

filea = open('GroceryList.txt', 'a')
fr = open('GroceryList.txt', 'r')
filer = open('GroceryList.txt', 'r')

runinng = True
while runinng:
  a = easygui.choicebox('Do you want to add, delete, or see your list?',
                choices = ['add', 'delete', 'see', 'quit'])
 if a == 'add':  # Here number of spaces from line starting to `if` is 5, see next note to error
   ad = easygui.enterbox('What do you want to add')
   filea.write(ad)
   filea.write('\n')
filea.close()
filea = open('GroceryList.txt', 'a')


elif a == 'delete':  # Here number of spaces from line starting to `elif` is 4, but before it was 5, maybe this is error
   rn = easygui.enterbox('What are you going to delete?')
   rl = fr.readlines()
   for lines in fr:
       if rn in lines:
           line.split()
       fr.close()
       fr = open('GroceryList.txt', 'r')  # here 'r' is read permission, but for editing you need 'w' writing permision


elif a == 'see':  # And here number of spaces are 3.
  s = filer.readlines()
  easygui.msgbox(s)
  filer.close()
  filer = open('GroceryList.txt', 'r')

 elif a == 'quit':
  runinng = False
filea.close()
fr.close()
filer.close()

So your code would be:

import easygui

filea = open('GroceryList.txt', 'a')
fr = open('GroceryList.txt', 'r')
filer = open('GroceryList.txt', 'r')

runinng = True
while runinng:
    a = easygui.choicebox('Do you want to add, delete, or see your list?',
        choices = ['add', 'delete', 'see', 'quit'])
if a == 'add':
    ad = easygui.enterbox('What do you want to add')
    filea.write(ad)
    filea.write('\n')
    filea.close()
    filea = open('GroceryList.txt', 'a')


elif a == 'delete':
    rn = easygui.enterbox('What are you going to delete?')
    rl = fr.readlines()
    for lines in fr:
        if rn in lines:
            line.split()
        fr.close()
        fr = open('GroceryList.txt', 'w')

elif a == 'see':
    s = filer.readlines()
    easygui.msgbox(s)
    filer.close()
    filer = open('GroceryList.txt', 'r')

elif a == 'quit':
    runinng = False
    filea.close()
    fr.close()
    filer.close()

So error is in number of spaces and permisions.

UltraStudioLTD
  • 300
  • 2
  • 14