0

I need to read 4 specific lines of a file in python. I don't want to read all the file and then get four out of it ( for the sake of menory). Does anyone know how to do that? Thanks!

P. S. I used the following code but apparently it reads all the file and then take 4 out of it.

a=open("file", "r")
b=a.readlines() [c:d]
Hamed
  • 1
  • 1
  • How big is the file? – TigerhawkT3 Apr 26 '16 at 02:30
  • 2
    Possible duplicate of [How to jump to a particular line in a huge text file?](http://stackoverflow.com/questions/620367/how-to-jump-to-a-particular-line-in-a-huge-text-file) – Bahrom Apr 26 '16 at 02:33
  • 1
    They suggest using [`linecache`](https://docs.python.org/2/library/linecache.html) in that question. – Bahrom Apr 26 '16 at 02:33
  • which is not a great solution since it reads the whole file into memory ... but i suspect somewhere in that question there is indeed an answer that answers OP question – Joran Beasley Apr 26 '16 at 02:34
  • Ah I see, thanks for clarifying @JoranBeasley – Bahrom Apr 26 '16 at 02:35
  • In case you're interested: [islice vs. linecache](http://stackoverflow.com/questions/19189961/python-fastest-access-to-line-in-file.) – l'L'l Apr 26 '16 at 02:46

2 Answers2

2

you have to read at least to the lines you are interested in ... you can use islice to grab a slice

interesting_lines = list(itertools.islice(a,c,d))

but it still reads up to those lines

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

Files, at least on Macs and Windows and Linux and other UNIXy systems, are just streams of bytes; there's no concept of "line" in the file structure, just bytes that happen to represent newline characters. So the only way to find the Nth line in the file is to start at the beginning and read until you've found (N-1) newlines. You don't have to store all the content you scan through, but you do have to read it.

Then you have to read and store from that point until you find 4 more newlines.

You can do this in Python, but it's not clear to me that it's a win compared to using the straightforward approach that reads more than it needs to; feels like premature optimization to me.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • It's not necessarily true that you have to read the whole file. If you know the length of the lines, you can seek to the appropriate spot and just read the four lines. – BallpointBen Apr 26 '16 at 02:43