python 如何删除文件中的特定行

python how to remove spacific line in file

我正在尝试从文件中删除。

这就是我想要做的。

1.importing Easygui

2.Put三种模式加退出

  1. 3种模式是(查看、添加、删除)

除删除部分外一切正常。 这就是我的

    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()

不工作的部分是:

    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')

正如其他人在评论中提到的,只是提供一个小脚本来消除您可能仍然存在的疑虑

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)

当你确定你拥有所有内容时重命名文件。

我在您的代码中注意到一件事:

导入 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()

因此您的代码将是:

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()

因此 spaces 的数量和权限存在错误。