Python 为什么我会得到列表索引错误?
我的文本文件有6行,但每次运行两次verify都会得到:Python 为什么我会得到列表索引错误?,python,Python,我的文本文件有6行,但每次运行两次verify都会得到: File "verifier.py", line 34, in <module> verify(i) File "verifier.py", line 27, in verify real_account = account[acc_no] IndexError: list index out of range 您正到达帐户没有有效的附件号索引的位置。尝试捕捉错
File "verifier.py", line 34, in <module>
verify(i)
File "verifier.py", line 27, in verify
real_account = account[acc_no]
IndexError: list index out of range
您正到达
帐户
没有有效的附件号
索引的位置。尝试捕捉错误并查看这些值的实际值:
try:
real_account = account[acc_no]
except IndexError:
print(acc_no, account)
我想你会发现那个账户不是你想象的那样 函数读取从当前位置到文件末尾的所有行。当您第二次调用verify()
时,您已经在文件的末尾,因此没有任何内容可供读取
您需要首先返回到文件的开头
def verify(acc_no):
read_file.seek(0)
account = read_file.readlines()
...
但是,您可以只读取一次文件并将其保存在全局变量中,而不是重复读取该文件。然后每次都可以重用该变量
with open("accounts.txt", "r") as read_file:
account = read_file.readlines()
您似乎只打开了一次read_文件,然后每次进入verify()函数时都试图使用read_file.readlines()读取其全部内容 好的,readlines读取整个文件,因此如果不重新打开文件,它将无法再次工作。我错过什么了吗? 说: 如果要读取列表中文件的所有行,也可以使用 列表(f)或f.读线()
当您试图获取位置本身不存在的位置值时,列表将引发索引器。所以问题是
账号在账户中不存在
read\u file.readlines()
将返回一个空列表,则会引发异常李>
现在我来解决这个问题
import random
import time
import json
import threading
import requests
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from colorama import init
from colorama import Fore, Back, Style
from datetime import datetime
init(autoreset=True)
config = json.load(open("config.json"))
times = int(config['amount'])
delay = int(config['verifier_delay'])
username = config['username']
password = config['password']
read_file = open("accounts.txt", "r")
account = read_file.readlines()
acc_len = len(account)
acc_no=0
def verify(acc_no):
if acc_no >= acc_len:
print('Account number does not exist in file. ', acc_no)
return
real_account = account[acc_no]
username = real_account[0:real_account.find(":")]
password = real_account[real_account.find(":")+1:]
print(username + password)
print(acc_no)
for i in range(times):
verify(i)
time.sleep(5)
干杯 你试过使用调试器吗?你在那里看到了什么?我确实捕捉到了错误,当文本文件有六行@101时,如果我添加with语句我得到:Traceback(最近一次调用):verify(I)read_file.seek(0)ValueError:I/O操作在关闭的文件上。我在代码中没有看到任何
read_file.close()
,我不明白这是怎么回事。你不应该同时使用我的两种解决方案,选择一种。要么读取一次文件并将其保存在变量中,要么在每次读取文件之前使用seek(0)
。我仍然会使用with语句获取超出范围的索引,我几乎可以肯定,尽管打开文件或real\u account=account[1]不会有问题应该返回一个索引错误,所以如果我将打开的文件添加到验证循环中,问题应该得到解决?我几乎可以肯定这与acc\u no有关,就像我刚做real\u account=account[1]时一样,它工作正常
import random
import time
import json
import threading
import requests
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from colorama import init
from colorama import Fore, Back, Style
from datetime import datetime
init(autoreset=True)
config = json.load(open("config.json"))
times = int(config['amount'])
delay = int(config['verifier_delay'])
username = config['username']
password = config['password']
read_file = open("accounts.txt", "r")
account = read_file.readlines()
acc_len = len(account)
acc_no=0
def verify(acc_no):
if acc_no >= acc_len:
print('Account number does not exist in file. ', acc_no)
return
real_account = account[acc_no]
username = real_account[0:real_account.find(":")]
password = real_account[real_account.find(":")+1:]
print(username + password)
print(acc_no)
for i in range(times):
verify(i)
time.sleep(5)