Q. Consider the file 'poem.txt' and predict the output of following code fragments if the file has been opened in filepointer file1 with code:

file1 = open (“E\\mydata\\poemBTH.txt”, ‘r+’)

(a)

print ("A. Output 1")

print (file1.read())

print ()

(b)

print ("B. Output 2")

print (file1.readline())

print ()

(c)

print ("C. Output 3")

print (file1.read(9))

print ()

(d)

print ("D. Output 4")

print (file1.readline(9) )

(e)

print ("E. Output of Readlines function is")

print (file1.readlines() )

print ()

NOTE: Consider the code fragments in succession, i.e., code (b) follows code (a), which means changes by code (a) remain intact. Similarly, code (e) follows (a) and (b), and so on.


Answer :-

Output :-

A. Output 1
God made the Earth;
Man made confusing countries.
And their fancy-frozen boundaries.
But with unfound boundless Love
I behold the borderland of my India
Expanding into the world.
Hail, mother of religions, lotus, scenic beauty, and sages!

B. Output 2


C. Output 3


D. Output 4

E. Output of Readlines function is
[]

>>> 

This is because After running part A the file pointer is at the last character of the file. So, after that, there is nothing to read and so on.

7 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post