python file.write()不会创建新行

时间:2017-10-19 20:22:53

标签: python python-3.6

我是Python的新手,我正在学习如何编写/阅读文本文件here的教程。但是我遇到了一个问题,当写一个文件时,它不会创建另一行,而只是在第一行之后直接写入。我在下面附上我的代码,非常感谢任何帮助!

   import os
import sys
def generate_cdat():
    file = open("documents/pytho/login/cdat.txt", "w")
    file.write("username[usr]")
    file.write("userpass[1234]")
    file.close()
def getCredentials(checkUsrName, checkUsrPass):
    file = open("documents/pytho/login/cdat.txt", "r")
    recievedUsrName = file.readline(1)
    recievedUsrPass = file.readline(2)
    if checkUsrName in recievedUsrPass:
        print("recieved username")
print("started program")
print("checking for constant data file")
path = "cdat.txt"
if os.path.exists(path):
    print("Constant data found, setting up")
else:
    print("Constant data not found, creating constant data.")
    generate_cdat()
print("starting login")
logingIn = True
while logingIn == True:
    getUsrName = input("Enter username: ")
    getUsrPass = getpass.getpass("Enter password: ")
    checkCredentials(getUsrName, getUsrPass)

再次感谢, 最大!<​​/ P>

1 个答案:

答案 0 :(得分:3)

尝试在字符串

的末尾添加\ n