Computer Science
Consider the file poemBTH.txt given above (in previous question). What output will be produced by following code fragment ?
obj1 = open("poemBTH.txt", "r")
s1 = obj1.readline()
s2.readline(10)
s3 = obj1.read(15)
print(s3)
print(obj1.readline())
obj1.close()
Python
Python File Handling
3 Likes
Answer
The code will result in an error because at line 3 there is a syntax error. The correct syntax is s2 = obj1.readline()
.
Working
The corrected code will be:
obj1 = open("poemBTH.txt", "r")
s1 = obj1.readline()
s2 = obj1.readline()
s3 = obj1.read(15)
print(s3)
print(obj1.readline())
obj1.close()
And their fancy
-frozen boundaries.
obj1 = open("poemBTH.txt", "r")
— This line opens the file namedpoemBTH.txt
in read mode ("r") and assigns the file object to the variableobj1
.s1 = obj1.readline()
— This line reads the first line from the file obj1 and assigns it to the variables1
.s2 = obj1.readline()
— This line reads the next line from the file obj1, starting from the position where the file pointer currently is, which is the beginning of the second line (from the previous readline() call). Then assigns it to the variables2
.s3 = obj1.read(15)
— This line reads the next 15 characters from the file obj1, starting from the position where the file pointer currently is, which is the beginning of third line (from the previous readline() call) and assigns them to the variables3
.print(s3)
— This line prints the contents of s3.print(obj1.readline())
— This line attempts to read the next line from the file obj1 and print it. However, since the file pointer is already ahead by 15 characters (from the previous read(15) call), this line will start reading from where the file pointer is currently positioned i.e., from "-" up to end of line.obj1.close()
— This line closes the file obj1.
Answered By
1 Like
Related Questions
How are following codes different from one another ?
(a)
my_file = open('poem.txt', 'r') my_file.read()
(b)
my_file = open('poem.txt', 'r') my_file.read(100)
If the file 'poemBTH.txt' contains the following poem (by Paramhans Yoganand) :
God made the Earth; Man made confining 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!
Then what outputs will be produced by both the code fragments given in question1.
Write code to open file contacts.txt with shown information and print it in following form :
Name: <name> Phone: <phone number>
Consider the file "poemBTH.txt" and predict the outputs of following code fragments if the file has been opened in filepointer file1 with the following 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 when code (b) is executing. Similarly, code (c) follows (a) and (b), and so on.