无法使用 readlines() 索引超出范围错误同时读取两行
Can't read two lines at the same time using readlines() index out of range error
我有一些文本文件包含 7 行,当我尝试使用以下代码打印第 3 行和第 4 行时,它打印第 3 行然后给我一个索引超出范围错误
for root, subFolders, files in os.walk(folder):
for file in files:
if file.endswith('Init.txt'):
with open(os.path.join(root, file), 'r') as fRead:
line_3 = fRead.readlines()[3]
line_4 = fRead.readlines()[4]
print line_3
print line_4
然而,当我 运行 以下代码之一时,我没有收到任何错误
代码 1:第 3 行打印正确
for root, subFolders, files in os.walk(folder):
for file in files:
if file.endswith('Init.txt'):
with open(os.path.join(root, file), 'r') as fRead:
line_3 = fRead.readlines()[3]
print line_3
代码 2:第 4 行打印正确
for root, subFolders, files in os.walk(folder):
for file in files:
if file.endswith('Init.txt'):
with open(os.path.join(root, file), 'r') as fRead:
line_4 = fRead.readlines()[4]
print line_4
好像不能同时读两行。这太令人沮丧了!
如文档中所述:
Help on built-in function readlines:
readlines(hint=-1, /) method of _io.TextIOWrapper instance
Return a list of lines from the stream.
hint can be specified to control the number of lines read: no more
lines will be read if the total size (in bytes/characters) of all
lines so far exceeds hint.
一旦您消耗完所有行,下一次调用 readlines
将为空。
更改函数以将结果存储在临时变量中:
with open(os.path.join(root, file)) as fRead:
lines = fRead.readlines()
line_3 = lines[3]
line_4 = lines[4]
print line_3
print line_4
方法 readlines()
读取文件中的所有行,直到到达 EOF(文件末尾)。
"cursor" 位于文件末尾,随后调用 readlines()
不会产生任何结果,因为直接找到了 EOF。
因此,在 line_3 = fRead.readlines()[3]
之后,您已经使用了整个文件,但只存储了文件的第四 (!) 行(如果您从 1 开始计算行数)。
如果你这样做
all_lines = fRead.readlines()
line_3 = all_lines[3]
line_4 = all_lines[4]
您只阅读了一次文件并保存了您需要的所有信息。
我有一些文本文件包含 7 行,当我尝试使用以下代码打印第 3 行和第 4 行时,它打印第 3 行然后给我一个索引超出范围错误
for root, subFolders, files in os.walk(folder):
for file in files:
if file.endswith('Init.txt'):
with open(os.path.join(root, file), 'r') as fRead:
line_3 = fRead.readlines()[3]
line_4 = fRead.readlines()[4]
print line_3
print line_4
然而,当我 运行 以下代码之一时,我没有收到任何错误
代码 1:第 3 行打印正确
for root, subFolders, files in os.walk(folder):
for file in files:
if file.endswith('Init.txt'):
with open(os.path.join(root, file), 'r') as fRead:
line_3 = fRead.readlines()[3]
print line_3
代码 2:第 4 行打印正确
for root, subFolders, files in os.walk(folder):
for file in files:
if file.endswith('Init.txt'):
with open(os.path.join(root, file), 'r') as fRead:
line_4 = fRead.readlines()[4]
print line_4
好像不能同时读两行。这太令人沮丧了!
如文档中所述:
Help on built-in function readlines:
readlines(hint=-1, /) method of _io.TextIOWrapper instance Return a list of lines from the stream.
hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.
一旦您消耗完所有行,下一次调用 readlines
将为空。
更改函数以将结果存储在临时变量中:
with open(os.path.join(root, file)) as fRead:
lines = fRead.readlines()
line_3 = lines[3]
line_4 = lines[4]
print line_3
print line_4
方法 readlines()
读取文件中的所有行,直到到达 EOF(文件末尾)。
"cursor" 位于文件末尾,随后调用 readlines()
不会产生任何结果,因为直接找到了 EOF。
因此,在 line_3 = fRead.readlines()[3]
之后,您已经使用了整个文件,但只存储了文件的第四 (!) 行(如果您从 1 开始计算行数)。
如果你这样做
all_lines = fRead.readlines()
line_3 = all_lines[3]
line_4 = all_lines[4]
您只阅读了一次文件并保存了您需要的所有信息。