【发布时间】:2019-12-06 06:57:17
【问题描述】:
当我运行代码时,我遇到了以下错误:
IndexError: 列表索引超出范围
我的代码有什么问题?
fin = open('words.txt')
for line in fin:
word = line.strip()
if len(word) > 20:
print(word)
print(fin.readlines()[1]) #It is in this line that the error report shows
【问题讨论】:
-
使用上下文管理器来处理文件。
readlines()不会产生任何结果,因为您位于文件的末尾。请参阅:ericlippert.com/2014/03/05/how-to-debug-small-programs。你能解释一下你想用这段代码做什么吗?
标签: python python-3.x