Last answered:

04 Nov 2021

Posted on:

03 Nov 2021

0

Files and functions _ Retrieving the second or third line output

Hi

From the underlining code. After obtaining the first line output. Going further, how do you retrieve the second or third line.



f = open('kipling.txt','r')

print(f.readline())
f.close()
print()





Files and fuctions

1 answers ( 0 marked as helpful)
Instructor
Posted on:

04 Nov 2021

2

Hey Ashakah,

Let me make use of the content variable that Giles defines at 7:55. If you want to obtain the variables line by line, you can iterate through that variable in, for example, the following way:

for i in content:
    print(i)

If you want a specific line, then you can type

print(content[# your index #])

where you replace # your index # with the index of the line that you want to print out.

Hope this helps!

Kind regards,
365 Hristina

Submit an answer