Cannot find what I'm sure is a simple command... please help

Sean 'Shaleh' Perry shalehperry at attbi.com
Sun Mar 31 18:33:04 EST 2002


On 31-Mar-2002 A. Jones wrote:
> I seem to have either overlooked, or completely missed a function that
> allows one to open a specific line of a file.
> 
> I've read through my Learning Python book, and read the relevent
> module docs (anything having to do with files, and the __builtins__).
> I must have overlooked such a function.
> 
> I thought the seek() function might work, but it only seems to give me
> a byte offset in the file, and not a line number.
> 

there is no way to do this with the libraries shipped with python.

An obvious implementation is:

def get_desired_line(filename, desired_line):
  fp = open(filename)
  lines = fp.readlines()
  return lines[desired_line - 1]







More information about the Python-list mailing list