1

I am new to Python and am following a tutorial on how to write/read text files here. But I ran into an issue where when writing a file, it does not create another line, but just writes directly after the first one. I attached my code below, any help is appreciated greatly!

   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)

1 Answer 1

4

Try adding \n at the end of the string

Sign up to request clarification or add additional context in comments.

1 Comment

That worked! Thanks for the help, that was kind of stupid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.