Binary File Operations in Python
Binary File Operations in Python
To implement a function `CreateFile()` for creating and appending records into a binary file 'Book.dat', start by defining the file structure [BookNo, Book_Name, Author, Price]. Open the file in binary append mode to safely add records without overwriting existing ones. Prompt the user to enter book details, then use the `pickle` module to serialize this data and append it to the file. Include error handling to manage any potential file I/O issues and ensure proper closure of the file after appending records .
To search and display bus details based on a destination from a binary file 'Bus.Dat', implement a function that opens the file in binary read mode. For each record, use the `pickle` module to decode and read the bus data, structured as [Bus Number, Bus Starting Point, Bus Destination]. Iterate through the records and compare the 'Bus Destination' field with the target destination 'Cochin'. When a match is found, display the details of that bus. This method necessitates handling I/O errors and ensuring the file is correctly closed after reading .
To retrieve and display coach information for a specific sport from a binary file 'sport.dat', create a function `countrec(sport name)`. Open the file in binary mode and iterate through the records using the `pickle` module. Each record is structured as [sport name, coach name]. Match the 'sport name' from the record with the passed parameter. If a match is found, print the 'coach name'. Implement proper exception handling to address any errors during file operations and ensure the file is closed after processing .
To count and display employees with a salary above 20000 from a binary file 'salary.DAT', you can create a function `countrec()`. Open the file in binary read mode and iterate through its records using the `pickle` module for decoding. For each record, which is structured as [employee id, employee name, salary], check if the 'salary' exceeds 20000. Count such records, and for those meeting the criteria, display the employee's id, name, and salary. Properly manage file access and error handling to prevent potential I/O issues .
To display details of products from a binary file 'product.dat' given a product code, implement a function `searchprod(pc)`. Open the file in binary read mode. For each record, decode it using the `pickle` module, and check if the 'product code' matches the code provided as an argument. Display the complete details of any matching product, including both 'product code' and 'product price'. Handle potential exceptions to deal with file reading errors and ensure the file is properly closed after the search operation .
To modify a route name in a binary file based on a given route number, define a function `routechange(route number)`. Open the file 'route.dat' in binary read-write mode and iterate through its records. For each record, decode it using the `pickle` module and check if the 'route number' matches the provided parameter. Upon finding a match, prompt the user to input a new route name and update the record with this change. Re-serialize the modified record and write it back to the file. Ensure that the changes are saved by properly closing the file after operations and handle exceptions to manage any I/O errors .
To write a function `addrec()` in Python for adding new student records to a binary file 'STUDENT.dat', first open the file in binary append mode to prevent overwriting existing data. Define the file structure, e.g., [Roll Number, Student Name]. Prompt the user to input new student details and append these details following the defined structure. Use the `pickle` module to serialize the new records before appending them. Ensure the function is equipped with error handling mechanisms to manage potential issues during file operations .
To search and display a product's details based on its code from a binary file 'product.dat', use a function `searchprod(pc)`. Open the file in binary read mode and iterate over each record using the `pickle` module for deserialization. Check each record's 'product code' against the parameter 'pc'. If a match is found, display the full details of that product, including 'product code' and 'product price'. Implement exception handling to ensure any file operation errors are managed and close the file after completing the search process .
To display student details who scored above 75% from a binary file 'STUDENT.DAT', write a function `countrec()`. Open the file in binary read mode and employ the `pickle` module to deserialize each student record structured as [admission_number, Name, Percentage]. As you process each record, compare the 'Percentage' field against 75. For records exceeding this threshold, display the student's admission number, name, and percentage. Tally these high scorers to report their count, ensuring to handle file errors gracefully and closing the file afterward .
To implement a Python function to count the number of books by a specific author in a binary file 'Book.dat', you can use the following approach: Define a function `CountRec(Author)` that opens 'Book.dat' in binary read mode. Utilize a loop to read each record, decode it using the `pickle` module, and check if the 'Author' field matches the provided parameter. Maintain a counter to track the number of matches and return this count after processing all records. The function must ensure proper exception handling to manage potential file I/O errors and use binary reading methods to accommodate the file's specific binary structure .