Getting a list index out of range error for 'level = line[3].rstrip()'. Have double-checked everything, I print the list right before and over 4 values (of which I am trying to get the fourth) are shown. Code & traceback below, thanks!
Code:
async def lbcmd(ctx):
el = []
for file in os.scandir(lpath):
if file.path.endswith(".txt"):
with open(file, 'r') as f:
line = f.readlines()
level = line[3].rstrip()
xp = line[0].rstrip()
exp = {
"user": file.name.strip(".txt"),
"exp": xp,
"level": level
}
el.append(exp)
el.sort(reverse=True, key=operator.itemgetter('exp'))
desc = ("""
""")
for exp in el:
desc = desc + f"""Level {exp['level']} at {exp['exp']} exp - <@{exp['user']}>
"""
embed=discord.Embed(title=f"Level leaderboard", description=desc, color=0x2f3136)
await ctx.send(embed=embed)
Traceback:
Traceback (most recent call last):
File "C:\Users\jakec\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\(A) Me\Coding\Python\jcjakec\Bots\conceptbot\conceptbot.py", line 144, in lbcmd
level = line[3].rstrip()
IndexError: list index out of range
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\jakec\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\jakec\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\jakec\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IndexError: list index out of range```