×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

readline() and specifying line number

readline() and specifying line number

readline() and specifying line number

(OP)
A very basic question... I'm new to Python...

Using repeated calls to:

 line=input.readline()

I can read succesive lines from a text file. Since each time I call this function I get the next line, there must be a poiter somewhere being incremented. Is there a way to manipulate this pointer such that I can instead specify the line I want to read (say line 5 or line 10) Of course I could just run the above x times to capture line x, but that seems very wasteful...

Cheers,

Ben

RE: readline() and specifying line number

Have you tried reading in the entire file? Not practical for massive files I know...

f = file('filename', 'r').readlines()
print f[linenumber]

you can also iterate over the file line by line:

for l in f:
    print l

May use lots of memory if you have a BIG file - but it sure is quick.

RE: readline() and specifying line number

There is a Seek Method but it is Byte based not line based. If your file has lines of a known/constant byte length, this will work.

The problem with something to move to a specific line is that the program has know way to know how many bytes to skip over to get to the line you want. The only way to know this is to read the file into a buffer until it gets to the line you are looking for. This is exactly the same thing that you do with your repeated calls to Readline().

"... isn't sanity really just a one trick pony anyway?!  I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick

RE: readline() and specifying line number

(OP)
"The only way to know this is to read the file into a buffer until it gets to the line you are looking for"

Good point. So I could puttogether a simple function to read a specified line, but it would do it by reading in every preceeding line and counting them as it went.

Cheers,

Ben

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close