2
I’m trying to use the readlines, but it makes a mistake that I don’t understand the reason, see:
arquivo = open("cadastros.txt", "r")
loginarquivo = arquivo.readlines()[0]
senhaarquivo = arquivo.readlines()[1]
arquivo.close()
At this point when I give the print in the loginarquivo, return is the first line of the file cadastros.txt and works normally. But the second variable senhaarquivo cannot read the second line of the file cadastros.txt returning an error of list index out of range.
And the curious thing is that if I put it in line senhaarquivo = arquivo.readlines()[1] the value of zero between [] asking him to read the first line of the file that just worked with the variable loginarquivo, it will return again the list index out of range.
Finally I realized that if I do it this way:
arquivo = open("cadastros.txt", "r")
loginarquivo = open("cadastros.txt", "r").readlines()[0]
senhaarquivo = open("cadastros.txt", "r").readlines()[1]
arquivo.close()
There it works but I do not think it is the best solution between these two, besides that it does not make sense to me that it works writing open("cadastros.txt, "r")" but it doesn’t work when I enter the variable arquivo this being the same value of it and I still believe that in this second way we would have several instances of the file cadastros.txt open in memory.
Could explain why the error occurs and how to make it work the most optimized way in this case the line senhaarquivo = arquivo.readlines()[1]?