If you don't want to read the entire file at once due to the large size of the file you are reading, use the code below:
fp = open("file")
for i, line in enumerate(fp):
if i == 25:
# # 26th line
elif i == 29:
# # 30th line
elif i > 29:
break
fp.close()
Note that i == n-1
is used to read the n
th line.
Use this method for Python 2.6 and later versions:
with open("file") as fp:
for i, line in enumerate(fp):
if i == 25:
# # 26th line
elif i == 29:
# # 30th line
elif i > 29:
break
© 2024 OneMinuteCode. All rights reserved.