{"id":3256,"date":"2022-07-22T23:54:33","date_gmt":"2022-07-22T18:24:33","guid":{"rendered":"https:\/\/cbsepython.in\/?p=3256"},"modified":"2022-09-17T00:30:44","modified_gmt":"2022-09-16T19:00:44","slug":"binary-file-handling-in-python-class-12-notes","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/binary-file-handling-in-python-class-12-notes\/","title":{"rendered":"Binary File handling in Python Class 12 notes"},"content":{"rendered":"<h1><span style=\"color: #000000;\">Binary File handling in Python Class 12 notes<\/span><\/h1>\n<h2><\/h2>\n<h2><span style=\"color: #000000;\">What is a binary file?<\/span><\/h2>\n<p><span style=\"color: #000000;\">A binary file is a file whose content is in a binary format consisting of a series of sequential bytes, each of which is eight bits in length.<\/span><\/p>\n<p><span style=\"color: #000000;\">Binary files are not human readable and require a special program or hardware processor that knows how to read the data inside the file.<\/span><\/p>\n<p><span style=\"color: #000000;\">When you write a computer program, data is held in variables and in more complex data structures such as arrays, dictionaries, or lists. A binary file allows you to store this data in a form that preserves the structures used in your program.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Serialisation (Pickling)<\/strong> is the process of converting an object (such as a dictionary of data) into binary sequences that can be stored in a file. When the file is accessed, the binary data is retrieved from the file and <strong>deserialised (Unpickling)<\/strong> into objects that are exact copies of the original information.<\/span><\/p>\n<p><span style=\"color: #000000;\">Python provides the <strong>pickle module<\/strong> to achieve the purpose of Pickling and Unpickling.<\/span><\/p>\n<p><span style=\"color: #000000;\">Pickle module provides:<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>dump() function:\u00a0<\/strong>We use dump() method to perform pickling operation on our\u00a0<strong>Binary Files.\u00a0<\/strong>It returns the object representation in byte mode. The dump() method belongs to pickle module.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Syntax:<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">import pickle<\/span><\/p>\n<p><span style=\"color: #000000;\">pickle.load(object,file)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\"><strong>load() function:\u00a0<\/strong>In pickle module, load() function is used to read data from a binary file or file object.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Syntax:<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">import pickle<\/span><\/p>\n<p><span style=\"color: #000000;\">pickle.load(file)<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Working with Binary File:<\/span><\/h2>\n<p><strong><span style=\"color: #000000;\">1) Writing to the Binary File<\/span><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import pickle\r\ndef writefile():\r\n    f=open(\"datafile.dat\", \"wb\")\r\n    list=[\"Apple\",\"Mango\", \"Banana\"]\r\n    pickle.dump(list,f)\r\n    f.close\r\nwritefile()\r\nprint(\"Writing done\")\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #000000;\">2) Reading from the Binary File<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import pickle\r\ndef readfile():\r\n    f=open(\"datafile.dat\",\"rb\")\r\n    result=pickle.load(f)\r\n    print(result)\r\n    f.close()\r\nreadfile()<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Binary File handling in Python Class 12 notes What is a binary file? A binary file is a file whose content is in a binary format consisting of a series of sequential bytes, each of which is eight bits in length. Binary files are not human readable and require a special program or hardware processor [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3256","post","type-post","status-publish","format-standard","hentry","category-cbse-sample-papers-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/3256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=3256"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/3256\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=3256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=3256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=3256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}