如何将用户输入存储到txt文件中?(Python 3)

How to store user input in txt file?(Python 3)

所以我正在编写一个程序,我做了一个将用户数据存储在 txt 中的功能 file.It 就像一个简单的注册 function.The 用户将用户名和密码提供给 store.Problem是我不能在文件中存储用户输入吗?

我的代码是:

def reguser():      #Function name.

    fwu = open('user.txt','w')     #create or open user.txt file.
    user = input("Username: ")     #takes the user input.e.g what username to register
    fwu.write(user)                #this command should write input into the file
    fwp = open('pass.txt','w')     #create or open pass.txt file.
    pas  = input ("Password: ")    #takes user input e.g what password to register
    fwp.write(pas)                 #write the password into the file
    print ("To check for registraion completion, please login.")
    askuser()    

所以我得到的是两个文本文件 user 和 pass 但它们是空的。 我究竟做错了什么?? 请不要告诉我使用模块进行注册。

问候 ali7112001

您没有 fwu.close()fwp.close()(您没有保存)。下次快速查找也会为您节省一些时间。 .write not working in Python

请尝试使用 raw_input() 而不是 input()

另请close()文件。