Binary File Handling in Python
Binary File Handling in Python
A function `CountRec(Author)` can be implemented to calculate the number of books by a specific author stored in a binary file 'Book.dat'. The function operates by opening the file in read (binary) mode and initializing a counter to zero. Using a loop, it reads each record from the file using `pickle.load()`, checks if the 'Author' field matches the input parameter, and increments the counter if it does. The function finally returns the counter value, which represents the number of books by the specified author .
The `add()` function enhances the file 'EMP.dat' by appending new employee records. It opens the file in append (binary) mode and repeatedly prompts the user to input values for employee ID, Name, and Salary. Each input set is packed into a list and serialized with `pickle.dump()`, appending the data to the file. The function includes a loop that allows the user to choose whether to add more records by entering a choice at the end of each iteration. The file is closed once the user opts not to add more records .
The function `countrec()` distinguishes and tallies students with percentages above 75 in 'STUDENT.DAT' by opening the file in read (binary) mode. It initializes a counter and iterates through each record using `pickle.load()`. During each iteration, it checks if the 'Percentage' field of the student record surpasses the threshold. If so, it increments the counter and prints the record details. The file is closed upon reaching EOF, after which it displays the total count of students meeting the criteria, providing an efficient way to tally based on a specific condition .
The `searchRollNo(r)` function locates a student's record in 'student.dat' by opening the file in read (binary) mode and using a loop to iterate through all records. It uses `pickle.load()` to deserialize each record and checks if the roll number in the record matches the parameter `r`. If a match is found, it sets a found flag to 1 and prints the student details. If the file reaches EOF without finding a match (indicating the flag remains 0), it outputs the message 'No record found', indicating the specific roll number was not located in the file .
The `frequency(name)` function determines how many times a customer has purchased an item by first opening the 'customerData.dat' file in read (binary) mode. It initializes a counter to zero and iterates through the records using `pickle.load()`. During each iteration, it checks if the 'customer name' field of the record matches the input parameter `name`. If a match is found, it increments the counter. The loop continues until an EOFError is raised, at which point the function closes the file and returns the counter value, representing the total purchases by the specified customer .
In Python, binary files are manipulated using the 'pickle' module to serialize and deserialize Python objects. For example, in managing a file 'Toy.dat' containing toy records with fields like Toy ID, Toy Name, Status, and MRP, a function `CreateFile()` is defined to input and store data records. The function opens the file in append (binary) mode, takes user input for each field, and uses `pickle.dump()` to serialize and write the record to the file. Similarly, `OnOffer()` function reads the file's contents to display only those records whose status is 'ON OFFER'. This is done by deserializing each record using `pickle.load()` and checking if the 'Status' field matches the specified criterion .
The `update(rollno)` function modifies a student's name in 'student.dat' by first opening the file in read (binary) and update mode ('rb+'). It uses a loop to iterate through the records, utilizing `pickle.load()` to deserialize each record and `fo.tell()` to get the current position in the file. When the record matching the specified 'rollno' is found, the name is updated based on user input, and the record is converted back to a tuple. The function then seeks back to the start position of the current record (`fo.seek(rpos)`) and overwrites it with the updated data using `pickle.dump()`. It finally closes the file after processing all records .
The function `Show()` identifies and displays records of employees designated as 'Salesman' by opening 'emp.dat' in read (binary) mode. It initializes a found flag to zero, then reads through the file using a loop with `pickle.load()`. For each record, it checks if the 'designation' field equals 'Salesman'. If a match is found, it sets the found flag to 1 and prints the record. If no matching records are found by the end of the file, it prints 'No record found' .
The function to append student records in 'student.dat' operates by opening the file in append (binary) mode using `pickle`. It ensures data integrity by collecting user input for each record field (Rollno, Name, Marks), organizing these inputs into a list, and then using `pickle.dump()` to append the serialized dictionary representation to the existing data in the file. By using append mode, the function preserves existing data in the file and maintains the correct order of records while adding new ones, thus ensuring that previous records are not overwritten or lost .
The `Display()` function filters products with a rating exceeding a threshold by opening 'product.dat' in read (binary) mode and iterating through its records. Each record is deserialized using `pickle.load()`, and its rating field is compared to the threshold value (10). If a record's rating exceeds the threshold, the function prints it and sets a flag to indicate that at least one matching record was found. If no matching records are found after traversing the entire file, it outputs 'No record found' .