0

I've been trying to make my program read a different line from a file each time the loop is completed. This works the first time but once the first loop is completed I am presented with IndexError: list index out of range. How can I fix this?

The file titled s is 18239 lines long and the file titled sp is 1000 lines long.

from itertools import count
import time
import webbrowser
import pynput
from pynput.keyboard import Key, Listener, Controller
import random
import string  
import secrets 



number = random.randint(0,18238)
number2 = random.randint(0,18238)
kb = Controller()
cout = 0
f = open('D:\Scripts\sp.txt', 'r')
fi = open('D:\Scripts\s.txt', 'r')
fil = open('D:\Scripts\s.txt', 'r')
while cout < 1000:
    linecount = random.randint(0,999)
    line = f.readlines()[linecount]
    lines = line.split()
    email = lines[0]
    password= lines[1]
    name = fi.readlines()[number]
    name2 = fil.readlines()[number2]
    firstname = name.rstrip("\n")
    lastname = name2.rstrip("\n")
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
RGCSERG
  • 39
  • 4
  • Where do you increment count ? – Charles Marcucci May 23 '22 at 11:34
  • Which specific line of code throws the error? What are the exact values used when the error occurs? – David May 23 '22 at 11:37
  • 2
    `linecount` has the wrong name. That is a random number and is not a linecount. Next, you call `f.readlines()` in a loop. After the first time, all lines are already read. What do you expect to happen during the second loop? There's nothing more to read. `lines` has the wrong name, again. It is not multiple lines. If `readlines()` returns an array of lines, `readlines()[linecount]` will already give you a single line. Basically: you do not want to read the full file `f` a thousand times. Just do it once, then work with the data you have. – Thomas Weller May 23 '22 at 11:37
  • @David line = f.readlines()[linecount] and then i also assume if it got to it name = fi.readlines()[number] and name2 = fil.readlines()[number2] – RGCSERG May 23 '22 at 11:39
  • @CharlesMarcucci it is incremented later this just is the only bit of code not working – RGCSERG May 23 '22 at 11:39
  • @ThomasWeller should switch readlines for readline – RGCSERG May 23 '22 at 11:42
  • `readline()` (without s) will not work either, as long as you want to access it with an index of `linecount` (or it will give you a single character, but likely it's index out of range) – Thomas Weller May 23 '22 at 11:44
  • @ThomasWeller yes your right i tried readline and and stated linecount after and it was out of range, is there a possible way of reading a specific line from a file one at a time? – RGCSERG May 23 '22 at 11:46
  • i could always use enumerate – RGCSERG May 23 '22 at 11:51
  • First, check what the value of `line` is each time through the loop, then see the linked duplicate. – Karl Knechtel May 23 '22 at 23:46

1 Answers1

0

now after some consideration I look at what was this issue, turns out python has issues re-reading files and so you have to put the readlines() outside the loop I ended up using read()

Here's what I did to fix the issue this version of the code worked perfectly with no issues (no re-reading files guys python will cry), this version also keeps the purpose of the code (to read a specific random line each loop)

import time
import webbrowser
import pynput
from pynput.keyboard import Key, Listener, Controller
import random
import string  
import secrets 




kb = Controller()
cout = 0
f = open('D:\Scripts\sp.txt', 'r')
fi = open('D:\Scripts\s.txt', 'r')
fil = open('D:\Scripts\s.txt', 'r')
lines = f.read().split("\n")   #outside the main loop to avoid index range error#
name = fi.read().split("\n")   #outside the main loop to avoid index range error#
while cout < 1000:
    linez = lines[random.randint(0,999)] # and then determine the line inside the loop#
    line = linez.split()
    email = line[0]
    password= line[1]
    names = name[random.randint(0,18238)] # and then determine the line inside the loop#
    namez = name[random.randint(0,18238)] # and then determine the line inside the loop#
    firstname = names.rstrip("\n")
    lastname = namez.rstrip("\n")
RGCSERG
  • 39
  • 4