简体   繁体   中英

Why doesn't my code read this line?

so I made a program that reads usernames with passwords (which are salted and hashed) from an SFTP server, and the user can log in securely. Users can also make their own accounts, but I am having a problem with the checking if the username already exists. I have managed to write the code correctly, but for some strange reason I can't find out why my code won't read one for loop, at least it doesn't print the "debug" string, example below. Any help would be appreciated!

def createUser():
f = ftp.open("paroolid.txt", "a+")
passListDecode = f.read().decode("UTF-8")
passList = passListDecode.splitlines()
newName = input("Uus kasutajanimi: ")
if len(newName) <= 0:
    print("Nimi peab olema vähemalt 1 täht!")
    createUser()
#This is the loop that won't be read
for line in passList:
    print("debug")
    pp = ""
    pp += line.split(",")[1].strip().split("-")[0].strip()
    pp += newName
    userInFile = hashlib.md5(pp.strip().encode()).hexdigest()
    if userInFile == line.split(":")[0].strip():
        print("Nimi juba olemas!")
        createUser()
#But this line is read successfully, like the above for loop is just being skipped.
newPass = getpass.getpass("Uus parool: ")

Everyone, I got it fixed. I learned that you cannot use the read() function when you're using the "a+" mode while opening a file, you'll need to use seek(0). With that the problem is answered, thanks to Mike Scotty for suggesting that the list might be empty, I didn't think of that before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM