title: Can you check whether the answer to the following question relating to python pickle/unpickle is correct or not-
body: <p>Question-
  A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].
  i. Write a user defined function CreateFile() to input data for a record and add to Book.dat .
  ii. Write a function CountRec(Author) in Python which accepts the Author name as parameter and count and return number of books by the given Author are stored in the binary file “Book.dat”</p>
  <p>Answer-</p>
  <pre><code>import pickle
  def createFile():
    fobj=open(&quot;Book.dat&quot;,&quot;ab&quot;)
    BookNo=int(input(&quot;Book Number : &quot;))
    Book_name=input(&quot;Name :&quot;)
    Author = input(&quot;Author: &quot;)
    Price = int(input(&quot;Price : &quot;))
    rec=[BookNo,Book_name,Author,Price]
    pickle.dump(rec,fobj)
    fobj.close()
  
  def CountRec(Author):
    fobj=open(&quot;Book.dat&quot;,&quot;rb&quot;)
    num = 0
    try:
      while True:
        rec=pickle.load(fobj)
        if Author==rec[2]:
          num = num + 1
    except:
      fobj.close()
    return num
  </code></pre>
  <p>My comments-
  If the binary file &quot;Book.dat&quot; has multiple records of the form [BookNo,Book_name,Author,Price] then the line
  rec=pickle.load(fobj)
  should load the entire file into the rec object and we cannot compare the author name to rec[2].
  So the above answer should be incorrect.</p>