IndexError: list index out of range
Error message:

The code is as follows:
with open(“linux_Yue_01.txt”,”r”) as testFile:
testfileList = [splitFileNameAndLabel(filename = v) for v testFile.read().split(“\n”)]
The picture is as follows:

Error analysis:
The “IndexError: list index out of range” error typically occurs in two ways:
The first possible case:
List [index] Index is out of range
The second possibility:
The list is empty, with no elements
Listing [0] will report this error.
Typically, there is a problem with externally entered data, so it is usually processed once after readlines or read.
This avoids empty lines, insufficient fields, and type-casting errors.
Solutions:
For the first case, check the code through debugging, not much to say.
In the second case, there are two ways:
The first: check the data read in is there a problem, such as read in the TXT file whether there is a blank line and so on, but I more advocate the second solution, in the code error correction.
with open(“linux_Yue_01.txt”,”r”) as testFile:
testfileList = []
for v testFile.read().split(“\n”)
If not v.strip(): # string remove the space is not empty
continue
testfileList.extend([splitFileNameAndLabel(v)])
Note: [splitFileNameAndLabel(v)] is square again, otherwise it would not be a nested list in a list
The picture is as follows:
Error message:

The code is as follows:
with open(“linux_Yue_01.txt”,”r”) as testFile:
testfileList = [splitFileNameAndLabel(filename = v) for v testFile.read().split(“\n”)]
The picture is as follows:

Error analysis:
The “IndexError: list index out of range” error typically occurs in two ways:
The first possible case:
List [index] Index is out of range
The second possibility:
The list is empty, with no elements
Listing [0] will report this error.
Typically, there is a problem with externally entered data, so it is usually processed once after readlines or read.
This avoids empty lines, insufficient fields, and type-casting errors.
Solutions:
For the first case, check the code through debugging, not much to say.
In the second case, there are two ways:
The first: check the data read in is there a problem, such as read in the TXT file whether there is a blank line and so on, but I more advocate the second solution, in the code error correction.
with open(“linux_Yue_01.txt”,”r”) as testFile:
testfileList = []
for v testFile.read().split(“\n”)
If not v.strip(): # string remove the space is not empty
continue
testfileList.extend([splitFileNameAndLabel(v)])
Note: [splitFileNameAndLabel(v)] is square again, otherwise it would not be a nested list in a list
The picture is as follows:

Read More:
- Index error: invalid index to scalar variable
- Python error type error: ‘range’ object does not support item assignment, solution
- Pre initialization of list content and length in Python
- Python common error: if using all scalar values, you must pass an index (four solutions)
- ValueError: not enough values to unpack (expected 2, got 0)
- Dataframe groupby custom aggregate function
- Several methods of deleting all empty lines in text under Linux
- Lock wait timeout exceeded — transaction and index
- [Python] pandas Library pd.to_ Parameter arrangement and example of Excel operation writing into excel file
- Common attributes and methods of list and map in Dar
- The built-in filter function in Excel can’t contain all the items
- Python — using Matplotlib to draw histogram
- Drawing cube with OpenGL
- session_ start(): open(SESSION_ FILE, O_ Rdwr)) failed: processing of permission denied (13)
- DB2 SQL error: sqlcode = – 803, sqlstate = 23505, sqlerrmc = 2 [solution]
- Flume monitors a single append file in real time
- Keyerror: “year not in index”
- 【Hackerrank】Reverse a doubly linked list
- Elasticsearch6. X invalid time range query bug
- Method of modifying file and folder permission by Chmod command in Linux