Python (Scripting) - Resolve "IndexError: list index out of range"
by
Jeremy Canfield |
Updated: February 25 2024
| Python (Scripting) articles
Let's say something like this is being returned.
Traceback (most recent call last):
File "/usr/local/scripts/backup_databases.py", line 250, in <module>
if object['Key'].split('.')[1]:
IndexError: list index out of range
Notice in this example that I got this on line 250 of my /usr/local/scripts/backup_databases.py script with command if object['Key'].split('.')[1]. This can be handled using try except error handling.
try:
object['Key'].split('.')[1]
except IndexError:
pass
else:
obj = object['Key'].split('.')[1]
Did you find this article helpful?
If so, consider buying me a coffee over at