[Tutor] Newbie with question about readfiles()
Sean 'Shaleh' Perry
shalehperry@attbi.com
Thu, 25 Apr 2002 12:57:00 -0700 (PDT)
On 25-Apr-2002 stuart_clemons@us.ibm.com wrote:
> Hi all:
>
> Just wondering how I would print only a given line in a text file. For
> example, I would like to print the 8th line in this multi-line file, foo.
> txt.
>
> in_file = open ("foo.txt", "r")
> for line in in_file.readlines():
> print line # I know this prints all lines
> print ???? # How do I print line 8 only ???
>
>
you have to count lines. There is no easy (and efficient) way.
wanted = 8
while wanted > 0:
line = in_file.readline() # note readline(), this reads one line at a time
wanted = wanted -1
print line