Reputation: 1
I'm trying to add a new section to my program that searches for bots on our server and deletes them, but I'm running into a show-stopping issue: Whenever the program gets to the second part (what I've added) the output for 'readFirst' is '[]'. When the next couple of lines run I get a "list index out of range" error for obvious reasons.
I have no idea why this is happening, as the readlines variable shouldn't be shared by both parts of the program.
Here is the code:
while runTimes >= 0:
#Cycles through .php files
openedProg = glob.glob('*.php')
openedProg = openedProg[currentPhp:currentPhp+1]
progInput = ''.join(openedProg)
if progInput != '':
theBot = open(progInput,'r')
#Singles out "$output" on this particular line and closes the process
readFourthFromBottom = theBot.readlines()
wholeLine = (readFourthFromBottom[-4])
output = wholeLine[4:11]
#Singles out "set_time_limit(0)"
readFirst = theBot.readlines()
print(readFirst)
wholeLine2 = (readFirst[0])
output2 = wholeLine2[7:24]
print(output2)
theBot.close()
if progInput == '':
currentPhp = -1
Upvotes: 0
Views: 123
Reputation: 387825
readlines
will read all lines from the IO object. So any subsequent try to read something from the file will yield EOF and as such the second readlines
will never return anything else.
You could seek to the beginning of the file before using readlines
again, or even better, just reuse the lines you read once. Unless you modify the readFourthFromBottom
(which is a terrible variable name btw.), which you don’t, there is no reason to read the lines again.
Upvotes: 0
Reputation: 26160
readlines()
reads all the way to the end of the file stream, which means that the cursor is at the end of the file. Really, the problem here is that readFourthFromBottom
is an inaccurate variable name. It should be all_lines
, because it will contain all the lines from the file in a list. Then you should just use that instead of trying to re-read the lines in to readFirst
.
Upvotes: 0
Reputation: 11935
You are reading all the lines the first time you do readlines().
The second time you call readlines, it starts at the end of the file, so there's nothing to read.
The readFirst
variable is worthless, you already have all the lines in the poorly named readFourthFromBottom
variable.
Upvotes: 1