Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python程序中将数据写入文件?_Python_Python 3.x - Fatal编程技术网

如何在python程序中将数据写入文件?

如何在python程序中将数据写入文件?,python,python-3.x,Python,Python 3.x,我写了一个代码来创建一个用户并登录,但是创建的用户只会在程序运行之前在内存中,当程序关闭时,用户就会被销毁。现在,如果我决定将用户名和密码存储到一个文件中,那么如何在下面的python代码中实现这一点。我只想将其存储到一个文件中,而不是读取或比较它。我刚开始学习Python,所以不知道任何高级或棘手的术语 编辑:我无法将用户名和密码写入文件 users = {} status = "" f = open('E:\\login_try2.txt','w') def displayMenu():

我写了一个代码来创建一个用户并登录,但是创建的用户只会在程序运行之前在内存中,当程序关闭时,用户就会被销毁。现在,如果我决定将用户名和密码存储到一个文件中,那么如何在下面的python代码中实现这一点。我只想将其存储到一个文件中,而不是读取或比较它。我刚开始学习Python,所以不知道任何高级或棘手的术语


编辑:我无法将用户名和密码写入文件

users = {}
status = ""

f = open('E:\\login_try2.txt','w')

def displayMenu():
    status = input("Are you registered user? y/n/q ")
    if status == "y":
        oldUser()
    elif status == "n":
        newUser()
    elif status == "q":
        exitCode()

def exitCode():
    exit()

def newUser():
    username = input("Enter username: ")     
    f.write(username)

    if username in users:
        print("\nUsername already exists!\n")
    else:
        password = input("Create Password: ")
        users[username] = password
        f.write(password)
        print("\nUser created\n")

def oldUser():
    login = input("Enter username: ")
    passw = input("Enter password: ")

    if login in users and users[login] == passw:
        print("\nLogin Successful!\n")
    else:
        print("\nUser doesnt exist or wrong password!\n")

while status != "q":
    displayMenu()

f.close()
问题是f.close语句仅在退出应用程序后执行,因此数据无法可靠地刷新到文件中

users = {}
status = ""

f = open('E:\\login_try2.txt','w')

def displayMenu():
    status = input("Are you registered user? y/n/q ")
    if status == "y":
        oldUser()
    elif status == "n":
        newUser()
    elif status == "q":
        exitCode()

def exitCode():
    exit()

def newUser():
    username = input("Enter username: ")     
    f.write(username)

    if username in users:
        print("\nUsername already exists!\n")
    else:
        password = input("Create Password: ")
        users[username] = password
        f.write(password)
        print("\nUser created\n")

def oldUser():
    login = input("Enter username: ")
    passw = input("Enter password: ")

    if login in users and users[login] == passw:
        print("\nLogin Successful!\n")
    else:
        print("\nUser doesnt exist or wrong password!\n")

while status != "q":
    displayMenu()

f.close()

可能最简单的解决方案是在newUser函数中将f.writepassword移到f.close之后。

答案很简单。未调用f.close,因为程序无法在文件中存储数据。因此,我在def exitCode中添加了f.close:


您看到的代码有什么问题?一个问题陈述和一个代码转储很好,但它没有告诉我们在哪里可以帮助澄清一个特定的问题。我无法将用户名和密码写入文件。可能会重复出现任何错误?我没有收到任何错误,程序运行正常