Skip to content
Advertisement

How to read specific lines from a file (by line number)?

I’m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?

Advertisement

Answer

If the file to read is big, and you don’t want to read the whole file in memory at once:

JavaScript

Note that i == n-1 for the nth line.


In Python 2.6 or later:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement