4 Replies - 478 Views - Last Post: 20 January 2012 - 09:41 AM Rate Topic: -----

Topic Sponsor:

#1 cupidvogel  Icon User is online

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 522
  • Joined: 25-November 10

Why is linecache necessary at all?

Posted 20 January 2012 - 07:41 AM

Hi, I am seeing that both pieces of code:

a = open("foo.txt","r")
print a.readlines()[0]



and

from linecache import *
print getline("foo.txt",1)



do the same thing, i.e print the first line of the given text file. So what is the necessity of the linecache module when the inbuilt file methods can do its task? Or is there some performance issues?

This post has been edited by cupidvogel: 20 January 2012 - 07:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why is linecache necessary at all?

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: Why is linecache necessary at all?

Posted 20 January 2012 - 08:38 AM

Quoth the manual, "The linecache module allows one to get any line from any file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file."
Was This Post Helpful? 1
  • +
  • -

#3 cupidvogel  Icon User is online

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 522
  • Joined: 25-November 10

Re: Why is linecache necessary at all?

Posted 20 January 2012 - 08:53 AM

So it means that using readlines() extracts all the lines from the file (which might be useless, as I am looking for one particular line only), then necessitates extracting the required line from the resulting list, while linecache extracts that line only, without extracting the other lines as well?
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: Why is linecache necessary at all?

Posted 20 January 2012 - 09:13 AM

The source for the linecache module is pretty short.
Was This Post Helpful? 1
  • +
  • -

#5 cupidvogel  Icon User is online

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 522
  • Joined: 25-November 10

Re: Why is linecache necessary at all?

Posted 20 January 2012 - 09:41 AM

Cool.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1