如何从文件中删除特定行

时间:2019-07-11 09:49:28

标签: python python-3.x

我正在尝试删除指定数量的名称,并指定了这些名称,但是我不知道该怎么做。

我尝试写而不是附加,删除和重写所有内容到文件。

#File setup
scoreboard_a = open("Dice scoreboard.txt","a")
scoreboard_r = open("Dice scoreboard.txt","r")

names_r = open("Authed Names.txt","r")
names_a = open("Authed Names.txt","a")

p1_password_a = open("Player 1 Passwords.txt","a")
p1_password_r = open("Player 1 Passwords.txt","r")

p2_password_a = open("Player 2 Passwords.txt","a")
p2_password_r = open("Player 2 Passwords.txt","r")

admin_names_a = open("Admin Names.txt","a")
admin_names_r = open("Admin Names.txt","r")

admin_pass_1_a = open("Admin Password 1.txt","a")
admin_pass_1_r = open("Admin Password 1.txt","r")

admin_pass_2_a = open("Admin Password 2.txt","a")
admin_pass_2_r = open("Admin Password 2.txt","r")

#Auth Vars
authed_names = []
p1_authed_password = []
p2_authed_password = []
admin_names = []
admin_pass_1 = []
admin_pass_2 = []

for name in names_r:
    authed_names.append(name)

for password in p1_password_r:
    p1_authed_password.append(password)

for password in p2_password_r:
    p2_authed_password.append(password)

for name in admin_names_r:
    admin_names.append(name)

for password in admin_pass_1_r:
    admin_pass_1.append(password)

for password in admin_pass_2_r:
    admin_pass_2.append(password)

admin_names = str(admin_names)
admin_pass_1 = str(admin_pass_1)
admin_pass_2 = str(admin_pass_2)

#Action
if action2 =="Deleting":
    number_of_names = input("How many names to delete: ")

    number_of_names = int(number_of_names)

    for number_of_names in range(number_of_names):
        print(authed_names)
        which_name = input("Which name: ")

        names_a.delete(which_name)
        p1_password_a.delete(which_name+"_pass_1")
        p2_password_a.delete(which_name+"_pass_2")

我希望它可以再运行其余的代码,但是我得到了:

  

回溯(最近通话最近一次):

     

文件“ \ student-server \ users $ _name_ \ Documents \ yr 9 \ cs \ Coding Work \ Dice rolling game.py”,第182行,在               names_a.delete(which_name)

     

AttributeError:'_io.TextIOWrapper'对象没有属性'delete'

0 个答案:

没有答案