0% found this document useful (0 votes)
11 views4 pages

BINARY

Binary files notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

BINARY

Binary files notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
BINARY FILES Topics Covered : Binary file: basic operations on a binary file: Open using file open modes (rb, rb+,wb,wb+, ab, ab+), Close a binary file, import pickle module, dump() and load() method, read, write/ereate, search, append and update operations in a binary file. oo00 Binary files store data in the binary format (0°s and 1’s) which is understandable by the machine. So when we open the binary file in our machine, it decodes the data and displays in a human-readable format. There are three basic modes of a binary file: read: This mode is written as rb write: This mode is written as wb append: This mode is written as ab The plus symbol followed by file mode is used to perform multiple operations together. For example, rb+ is used for reading the opening file for reading and writing. The cursor position is at the beginning when + symbol is written with file mode. To open a binary file follow this syntax: file = open(< For example lepath>, mode) f= open(“one.dat”,"1b”) Binary File Modes: File mode governs the type of operations read/write/append possible in the opened file, It refers to how the file will be used once its opened File Description Mode 1b Read Only: Opens existing file for read operation wb Write Only: Opens file for write operation. If file does not exist, file is created. If file exists, it overwrites data. ab Append: Opens file in write mode. If file exist, data will be appended at the end, tb+ Read and Write: File should exist, Both read and write operations can be performed wb+ Write and Read: File created if not exist, If file exist, file is truncated. ab+ Write and Read: File created if does not exist, If file exist data is truncated, Pickle Module: Python pickle is used to serialize and deserialize a python object structure. Any object on python can be pickled so that it can be saved on disk. Pickling: Pickling is the process whereby a Python object hierarchy is converted into a byte stream. It is also known as serialization Unpickling: A byte stream is converted into object hierarchy. To use the pickling methods in a program, we have to import pickle module using import keyword. Example: import pickle In this module,we shall discuss two functions of pickle module, which are: i) dump0:To store/write the object data to the file. ii) load():To read the object data from a file and returns the object data. Syntax: Write the object to the file: pickle. dump(objname, file-object ) Read the object from a file: pickle load(file-object) Write data to a Binary File: Example: import pickle list =[] # empty list while True: roll = input("Enter student Roll No:") sname=input("Enter student Name:") student={"roll":roll, "name":sname} #ereate a dictionary list.append(student) Hadd the dictionary as an element in the list choice=input("Want to add more record(y/n):") iffchoice=='n). break Jfile=open("student.dat”"wb") Hopen file in binary and write mode pickle.dump(list, fle) file.close() ouTPU Enter student Roll No: 1201 Enter student Name: Anil Want to add more recordtyin): y Enter student Roll No: 1202 Enter student Name: Sunil Want to add more record(y/n): n Read data from a Binary File: To read the data from a binary file, we have to use load() function Example: import pickle {file = open("student.dat”, "rb") list =pickle.load(file) print(list) file.close() OUTPUT: [{(roll'1201" name’ Anil’},{'roll’1202'’name''Sunil"}] Update a record in Binary File: def updated. name=input("Enter the name to be updated ") newstu=[] while True: try: stu=p.load(p) for iin stu: if i[1] lower ==name.lower() rno=int(input("Enter the updated Roll number") s=[rno,name] newstu.append(s) else: newstu.append(i) except break ficlose) fropen("student.dat”,"rb+") update) p.dump(newstuf) print(“Record updated") felosed OUTPUT: Enter the name to be updated Sunil Enter the updated Roll number 1204 Record updated Delete a record from binary file: import pickle def deletestudent(): roll=input(‘Enter roll number whose record you want to delete:') list = pickle.load(fw) found=0 Ist= [] for x in list ifroll not in x{'roll’. Ist.append(x) else: found=1 _fw=open("“student.dat”,"rb+") delestudent() pickle.dump (st) fiw.close() if found==1: print(“Record Deleted”) else: print(“Record not found") OUTPUT: Enter roll number whose record you want to delete:1201 Record Deleted

You might also like