为什么我写的文件不保存输入的文本到Python?

Why don't my written files not save the inputted text in Python?

我正在制作一个聊天机器人,如果 UserName.txt 文件不存在,用户做的第一件事就是输入他们的名字和喜欢的内容,但输入的文本不会保存。我做错了什么?

我已经尝试删除 UserLike 函数以查看是否会有所不同,但它似乎不起作用

def NameSay():

    UserName = input(": ")
    UserNameFile = open("UserName.txt", "w+")
    UserNameFile.write(UserName)
    print("So your name is "+ UserName + ", right?")
    NameConfirm = input(": ")

    if 'yes' in NameConfirm or 'Yes' in NameConfirm or 'right' in NameConfirm or 'Right' in NameConfirm or 'ya' in NameConfirm or 'Ya' in NameConfirm or 'yeah' in NameConfirm or 'Yeah' in NameConfirm:
        print("Good to meet you, " + UserName + ". I'm Ene, your virtual assistant, friend, coworker, whatever you need me to be!")
        print("Now, why don't you tell me a bit about yourself? What you like and all of that.")
        UserLike = input(": ")
        UserLikesFile = open("UserLikes.txt", "w")
        UserLikesFile.write(UserLike)
        print("Thank you! This is very interesting info.")

    if NameConfirm in ['no', 'No']:
        print("Oh? Then tell me what your name is.")
        NameSay()

if os.path.isfile('UserName.txt') == True:
    f = open("UserName.txt", "r")
    file_contents = f.read()
    welcomes = ["Welcome back, " + file_contents, "Hey-o! Good to see you again, " + file_contents]
    print("\n" + welcomes[random.randint(0,1)])
    Main_Menu()

if os.path.isfile('UserName.txt') == False:
    print("\nHey-o! I don't think we've met before! What\'s your name?")
    NameSay()

文本应该保存到写入的文件中,但文件最终是空白的。

我刚刚 运行 这个并且数据确实保存了,所以我不太确定你在说什么。但是我可以看到您在使用文件后没有按照您应该的方式关闭它们。

为了安全起见,您可以在写入函数后立即将输出刷新到文件中。

UserNameFile.flush()

此外,不要忘记关闭文件

UserNameFile.close()

我认为你的代码有两个问题:

1- 当您打开一个文件时,您应该关闭或刷新该文件以便能够读取该文件。我使用 with 语句进行文件管理。

2- 你正在递归地调用 NameSay(),如果一个人输入他的用户名错误 10 次,你的函数调用堆栈应该保存所有这些。因此,我编辑了最后一行不递归调用该函数的代码,并将 NameSay 函数编辑为 return TrueFalse 以确定它是否成功或不是

def NameSay():

    UserName = input(": ")
    with open("UserName.txt", "w+") as UserNameFile:
        UserNameFile.write(UserName)
    print("So your name is "+ UserName + ", right?")
    NameConfirm = input(": ")

    if 'yes' in NameConfirm or 'Yes' in NameConfirm or 'right' in NameConfirm or 'Right' in NameConfirm or 'ya' in NameConfirm or 'Ya' in NameConfirm or 'yeah' in NameConfirm or 'Yeah' in NameConfirm:
        print("Good to meet you, " + UserName + ". I'm Ene, your virtual assistant, friend, coworker, whatever you need me to be!")
        print("Now, why don't you tell me a bit about yourself? What you like and all of that.")
        UserLike = input(": ")
        with open("UserLikes.txt", "w") as UserLikesFile:
            UserLikesFile.write(UserLike)
        print("Thank you! This is very interesting info.")
        return True

    if NameConfirm in ['no', 'No']:
        print("Oh? Then tell me what your name is.")
        return False

if os.path.isfile('UserName.txt') == True:
    f = open("UserName.txt", "r")
    file_contents = f.read()
    welcomes = ["Welcome back, " + file_contents, "Hey-o! Good to see you again, " + file_contents]
    print("\n" + welcomes[random.randint(0,1)])
    Main_Menu()

if os.path.isfile('UserName.txt') == False:
    print("\nHey-o! I don't think we've met before! What\'s your name?")
    while(not NameSay())
        continue