Page 149 - PYTHON-12
P. 149

(f)  Which of the following modes is used for both writing and reading from a binary file?
                         (i)  wb+              (ii)  w               (iii)  wb              (iv)  w+
                     (g)  Which statement is used to retrieve the current position within the file?
                         (i)  fp.seek()        (ii)  fp.tell()       (iii)  fp.loc          (iv)  fp.pos
                     (h)  What happens if no arguments are passed to the seek() method?
                         (i)  file position is set to the start of file   (ii)  file position is set to the end of file
                        (iii)  file position remains unchanged       (iv)  results in an error
                      (i)  Which of the following modes will refer to binary data?
                         (i)  r                (ii)  w               (iii)  +               (iv)  b
                      (j)  Every record in a CSV file is stored in reader object in the form of a list using which method?
                         (i)  writer()         (ii)  append()        (iii)  reader()        (iv)  list()
                    Answers:  (a)  (ii)     (b)  (i)       (c)  (ii)     (d)  (ii)     (e)  (iii)     (f)  (i)
                              (g)  (ii)     (h)  (iv)      (i)  (iv)      (j)  (iii)

               SOLVED QUESTIONS

                 1.  Write a Python code to find the size of the file in bytes, number of lines and number of words.
                Ans.  # reading data from a file and find size, lines, words
                     f = open('Lines.txt', 'r')
                     str = f.read()
                     size = len(str)
                     print('size of file n bytes ', size)
                     f.seek(0)
                     L = f.readlines()
                     word = L.split()
                     print('Number of lines ', len(L))
                     print('Number of words ', len(word))
                     f.close()
                 2.  Consider the following code:
                      f = open("test", "w+")
                     f.write("0123456789abcdef")
                     f.seek(-3,2)              //Statement 1
                     print(f.read(2))          //Statement 2
                      Explain statement 1 and give output of statement 2.
                Ans.  Statement 1 uses seek() method that can be used to position the file object at a particular place in the
                    file.
                      It’s syntax is:
                        fileobject.seek(offset [, from_what])
                      So, f.seek(-3,2) positions the fileobject to 3 bytes before end of file.
                      Output of Statement 2 is:
                        de
                      It reads 2 bytes from where the file object is placed.
                   3.  Yogendra intends to position the file pointer to the beginning of a text file. Write Python statement for
           Computer Science with Python–XII  4.20  Ans.  F.seek(0)
                    the same assuming "F" is the Fileobject.


                 4.  Differentiate between file modes r+ and rb+ with respect to Python.
                Ans.  r+ opens a file for both reading and writing. The file pointer placed at the beginning of the file.
                      rb+ opens a file for both reading and writing in binary format. The file pointer is placed at the beginning

                    of the file.
                 5.  In which of the following file modes the existing data of the file will not be lost?

                      rb, ab, w, w+b, a+b, wb, wb+, w+, r+
                Ans.  In file modes rb, ab, a+b and r+, data will not be lost.

                      In file modes w, w+b, wb, wb+ and w+, data will be truncated i.e. lost.
   144   145   146   147   148   149   150   151   152   153   154