[Tutor] file question
Reggie Dugard
reggie@merfinllc.com
Tue Aug 5 09:38:01 EDT 2003
Joshua,
There's no direct way to get to a specific line in a file, but you can
do it without having to read in the entire file. If you're using Python
2.3 you can do the following:
for line_number, line in enumerate(open('filename')):
if line_number == desired_line:
break
else:
raise ValueError, 'File too short'
On Tue, 2003-08-05 at 05:24, jpollack@socrates.Berkeley.EDU wrote:
> Hi everyone,
>
> Is there a way to pull a specific line from a file without reading the
> whole thing into memory with .readlines()?
>
> I have a monstrous text file, but I can easily figure out the index
> numbers of the particular line I need without having to read the whole
> thing in? Is there a quick way to do this?
>
> I couldn't find anything in documentation specifically addressing this.
>
>
> Thanks!
> Joshua Pollack
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Reggie
More information about the Tutor
mailing list