尝试从“ txt”文件中删除字符串时出错,它只会删除所有内容。 -联系人列表程序

时间:2019-06-09 22:38:58

标签: python

我正在创建一个联系人列表/预订程序,可以为您创建新的联系人。将它们保存在“ txt”文件中。列出所有联系人,并删除现有联系人。好吧。在我的删除功能中,发生了一个错误,我不太清楚为什么。运行时外壳上没有提示错误。这是要询问用户要删除的联系人,在“ txt”文件中找到用户所说的内容。然后删除它。它可以轻松找到它,但是只会删除所有内容。

我尝试了各种方法来完成此操作,但没有任何效果?

import os, time, random, sys, pyautogui


#function for creating a new contact.
def new_contact():
    os.system('cls')


    name = str(input("Clients name?\n:"))

    name = name + " -"

    info = str(input("Info about the client?\n:"))

    #starts formatting clients name and info for injection into file.

    total = "\n\n"
    total = total + name
    total = total + " "
    total = total + info
    total = total + "\n"
    #Injects info into file.


    with open("DATA.txt", "a") as file:

        file.write(str(total))
        file.close

    main()

#function for listing ALL contacts made.
def list():
    os.system('cls')



    file = open("DATA.txt", "r")
    read = file.read()
    file.close


    #detects whether there are any contacts at all. If there are none the only str in the file is "Clients:"
    if read == "Clients:":
        op = str(input("You havn't made any contacts yet..\nDo you wish to make one?\n:"))


        if op == "y":
            new_contact()


        else:
            main()

    else:
        print (read)
        os.system('pause')
        main()


#Function for deleting contact
def delete_contact():
    os.system('cls')


    file = open("DATA.txt", "r")
    read = file.read()
    file.close


    #detects whether there are any contacts at all. If there are none the only str in the file is "Clients:"
    if read == "Clients:":
        op = str(input("You havn't made any contacts yet..\nDo you wish to make one?\n:"))


        if op == "y":
            new_contact()


        else:
            main()


    else:
        #tries to delete whatever was inputted by the user.
        file = open("DATA.txt", "r")
        read = file.read()
        file.close


        print (read, "\n")


        op = input("copy the Clients name and information you wish to delete\n:")


        file = open("DATA.txt", "w")
        var = read.replace((op), "")
        file.write(var)
        file.close
        os.system('pause')
        main()

        #with open("DATA.txt") as f:
            #reptext=f.read().replace(op, '')


        #with open("DATA.txt", "w") as f:
            #f.write(reptext)
            #main()


main()

希望从文本文件中删除用户作为变量op输入的任何内容,但是它只会删除所有内容。通过将文本文件中的所有内容都设置为str,并将用户在str中输入的内容替换为,可以使其工作。但是,它只是将文件中的所有内容替换为(什么都没有)。

这是.txt文件中的内容:

Clients:

Erich - The developer

Bob - the test dummy.

1 个答案:

答案 0 :(得分:0)

正如@rayryeng在他的评论中所说,该错误可能是由于您没有在代码的delete_contact()函数中正确关闭文件这一事实而产生的。整个功能应该是这样的:(顺便说一句,我删除了代码的主菜单功能,因为这则帖子将其保留在您的代码中

def delete_contact():
    os.system('cls')


    file = open("DATA.txt", "r")
    read = file.read()
    file.close()


    #detects whether there are any contacts at all. If there are none the only str in the file is "Clients:"
    if read == "Clients:":
        op = str(input("You havn't made any contacts yet..\nDo you wish to make one?\n:"))


        if op == "y":
            new_contact()


        else:
            main()


    else:
        #tries to delete whatever was inputted by the user.
        file = open("DATA.txt", "r")
        read = file.read()
        file.close() #Edited this to make the file close properly


        print (read, "\n")


        op = input("copy the Clients name and information you wish to delete\n:")


        file = open("DATA.txt", "w")
        var = read.replace((op), "")
        file.write(var)
        file.close() #Edited this to make it close properly
        os.system('pause')
        main()

但是,这还不是终点

如果您使用我上面发布的代码,则您的代码将无法运行。相反,它将具有先前的联系人(在删除之前),然后是联系人/客户端列表减去用户删除的联系人。要解决此问题,请在最后一次打开文件之前添加以下代码段:

file = open("DATA.txt", "w")
file.replace(read, "")
file.close()

此代码的作用是打开文档,将之前的内容替换为(什么都没有),然后关闭(正确)。

(顺便说一句好程序)