You are on page 1of 2
1. What isthe difference between readl ) and readlines() function? Solution. The read() reads from a file in read mode, and stores its contents in a string type variable. ‘The readlines() function, reads from a file in read mode and returns a list of all lines in the file 2 Differentiate etween the following {CBSE D 20151 © F=open(‘diary.txt', 'r") (fF open(*diary.txt*, tw") Solution. (File has been opened in read mode with file handle f (i File has been opened in write mode with file handle f jnetion? a 4. irs ese eave rnin nd reine JOM We ext tin in the fle ; i oma file in read mode ‘ Solution, The readfine() funtion roads fr ea a of ting YPC ae ng thre are no more ines, The returned dot SOT A sin hele The readlines() function, also reads from a file in read whitespaces. Solution, for Line dn File(*poen.txt") + print (1ine-strip()) va 8. What is the output of following code ? file("e:\\poem.txt", ) -readline().s “split() Recall that poem txt has some lading and traitin hi Solution. D7] 8 whitespaces, 9. What is the output of following code ? fille("e:\\poen.txt", "r") .readline() Solution nye 10. What is the output of following code ? fh = File(*poen. txt", "e*) size = len(fharead()) Print (fh.read(S)) Solution. No output Explanation. The fh.read( ) of line 2 will read the entire file content and place the file pointer at end of file. For the fh.read(), it will return nothing as there are no bytes to be read from EOF thus print) statement prints nothing, UL. Write a program to display all the records in a file along with linelrecord number. Solution. fh = open("Result det”, "r") count = @ while True = rec = fh.readline() if rec ==" break count = count +1 print(count, rec, end fh.close() # to suppress extra newline by print 13. The file “New.txt” contains the following : Better than Heaven or Arcadia I love thee, 0 my India! And thy love T shall give To every brother nation that lives. ne following cme Considering the given file, what output will be produced by the fol ‘ebja-open( "New. txt" gi = obji.readiine() <2 = obji.readline() 3 = obji.readline() 54 = obji-read(15) print(s4) ‘obji.close() “) and thy love T bove code is = ne saw tio codes (carefully got Solution, The output

You might also like