3 回答
TA贡献1818条经验 获得超8个赞
尝试这个
with open(r"SaveUsername.txt","w+") as SaveUsername:
NewUsername = input("Please enter a username: ")
SaveUsername.write(NewUsername)
with open(r"SavePassword.txt","w+") as SavePassword:
NewPassword = input("Please enter a password: ")
SavePassword.write(NewPassword)
with open(r"SaveUsername.txt" as SaveUsername:
print (SaveUsername.readline())
with open(r"SavePassword.txt") as SavePassword:
print (SavePassword.readline())
TA贡献1876条经验 获得超5个赞
首先,您应该在写入文件后关闭文件,然后再读取更改。您还应该使用with open(...)它,因为它是一种更好的做法(出于多种原因)。这是您应该使用的代码:
with open(r"C:\Users\Ty\Desktop\Login_sys\SaveUsername.txt", "w") as SaveUsername:
NewUsername = input("Please enter a username: ")
SaveUsername.write(NewUsername)
with open(r"C:\Users\Ty\Desktop\Login_sys\SavePassword.txt", "w") as SavePassword:
NewPassword = input("Please enter a password: ")
SavePassword.write(NewPassword)
with open(r"C:\Users\Ty\Desktop\Login_sys\SaveUsername.txt", "r") as SaveUsername:
print(SavePassword.readline())
with open(r"C:\Users\Ty\Desktop\Login_sys\SavePassword.txt", "r") as SavePassword:
print(SavePassword.readline())
添加回答
举报