为什么我的代码不读取这一行?
Why doesn't my code read this line?
所以我制作了一个程序,从 SFTP 服务器读取带有密码(经过加盐和哈希处理)的用户名,用户可以安全登录。用户也可以创建自己的帐户,但我在检查用户名是否已存在时遇到问题。我设法正确地编写了代码,但出于某种奇怪的原因,我无法找出为什么我的代码不会读取一个 for 循环,至少它不会打印 "debug" 字符串,如下例所示。如有任何帮助,我们将不胜感激!
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: ")
大家好,我搞定了。
我了解到在打开文件时使用 "a+" 模式时不能使用 read() 函数,您需要使用 seek(0)。
至此问题得到解决,感谢 Mike Scotty 建议列表可能为空,我之前没有想到。
所以我制作了一个程序,从 SFTP 服务器读取带有密码(经过加盐和哈希处理)的用户名,用户可以安全登录。用户也可以创建自己的帐户,但我在检查用户名是否已存在时遇到问题。我设法正确地编写了代码,但出于某种奇怪的原因,我无法找出为什么我的代码不会读取一个 for 循环,至少它不会打印 "debug" 字符串,如下例所示。如有任何帮助,我们将不胜感激!
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: ")
大家好,我搞定了。 我了解到在打开文件时使用 "a+" 模式时不能使用 read() 函数,您需要使用 seek(0)。 至此问题得到解决,感谢 Mike Scotty 建议列表可能为空,我之前没有想到。