Page 153 - PYTHON-12
P. 153

2.  Write a program to add two more employees details to the file "emp.txt" already stored in disk.
                 3.  How are the following codes different from one another?

                      (a)  fp = open("file.txt", 'r')
                        fp.read()
                      (b)  fp=open("file.txt", 'r')

                 4.  What is the output of the following code fragment? Explain.
                     fout = file("output.txt", 'w')
                     fout.write("Hello, world!\n")
                     fout.write("How are you?")
                     fout.close()
                     file("output.txt").read()
                 5.  Write the output of the following code with justification if the contents of the file ABC.txt are:
                      Welcome to Python Programming!
                     f1 = file("ABC.txt", "r")
                     size = len(f1.read())
                     print(size)
                     data = f1.read(5)
                     print(data)
                 6.  Anant has been asked to display all the students who have scored less than 40 for Remedial Classes. Write
                    a user-defined function to display all those students who have scored less than 40 from the binary file
                    "Student.dat".
                 7.  Give the output of the following snippet:
                     import pickle
                     list1, list2 = [2, 3, 4, 5, 6, 7, 8, 9, 10], []
                     for i in list1:
                         if (i%2==0 and i%4==0):
                             list2.append(i)
                     f = open("bin.dat","wb")
                     pickle.dump(list2, f)
                     f.close()
                     f = open("bin.dat", "rb")
                     data = pickle.load(f)
                     f.close()
                     for i in data:
                         print(i)
                 8.  Following is the structure of each record in a data file named "PRODUCT.DAT".

                      {"prod_code": value, "prod_desc": value, "stock": value}
                      The values for prod_code and prod_desc are strings, and the value for stock is an integer.
           Computer Science with Python–XII  4.24   9.  Given a binary file “STUDENT.DAT”, containing records of the following type:
                      Write a function in Python to update the file with a new value of stock. The stock and the product_code,
                    whose stock is to be updated, are to be inputted during the execution of the function.



                      [S_Admno, S_Name, Percentage]
                      Where these three values are:

                      S_Admno – Admission Number of student (string)
                      S_Name – Name of student (string)
                      Percentage – Marks percentage of student (float)

                      Write a function in Python that would read contents of the file “STUDENT.DAT” and display the details of
                    those students whose percentage is above 75.
   148   149   150   151   152   153   154   155   156   157   158