0% found this document useful (0 votes)
17 views1 page

Binary File Operations in Python

Uploaded by

fairnal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Binary File Operations in Python

Uploaded by

fairnal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1.A binary file “[Link]” has structure [rollno, name, marks].

i. Write a user defined function insertRec() to input data for a student and add to
[Link].
ii. Write a function searchRollNo( r ) in Python which accepts the student’s rollno
as parameter and searches the record in the file “[Link]” and shows the
details of student i.e. rollno, name and marks (if found) otherwise shows the
message as ‘No record found’.
[Link] a python program to append a new records in a binary file –“[Link]”.
The record can have Rollno, Name and Marks.
3.A binary file named “[Link]” has some records of the structure [EmpNo, EName,
Post, Salary]
(a) Create a binary file “[Link]” that stores the records of employees and display
them one by one.
(b) Display the records of all those employees who are getting salaries between
25000 to 30000.
4.A binary file “[Link]” has structure (admission_number, Name, Percentage).
Write a function countrec() in Python that would read contents of the file
“[Link]” and display the details of those students whose percentage is above
75. Also display number of students scoring above 75%.
5.A binary file named “[Link]” has some records of the structure [EmpNo, EName,
Post, Salary]
(a) Write a user-defined function named NewEmp() to input the details of a new
employee from the user and store it in [Link].
(b) Write a user-defined function named SumSalary(Post) that will accept an
argument the post of employees & read the contents of [Link] and calculate the SUM
of salary of all employees of that Post.

Common questions

Powered by AI

Defining functions like 'insertRec' and 'NewEmp' centralizes the process of data input and storage, promoting code reuse and maintainability. These functions abstract complex file operations, reduce redundancy, and ensure consistency in data handling, greatly simplifying the development and debugging process in managing binary file data.

To append new employee records to 'EMP.dat', you can use a function designed to capture employee details such as EmpNo, EName, Post, and Salary and store them in the file. These records can then be displayed individually by reading through the file and outputting each record one by one.

To filter and display employees earning between 25,000 and 30,000 from a binary file, you would read each employee record from 'EMP.dat' and check if the salary falls within the specified range. Only the employees with salaries in this range would be displayed.

You would write the function NewEmp() to add a new employee's data to 'EMP.dat'. This function would include data fields such as EmpNo, EName, Post, and Salary.

Binary files are preferred over text files in scenarios where data integrity and efficiency are crucial, such as storing structured records like student or employee databases where data types vary widely (integers, strings). They are more efficient in storage and retrieval operations, especially for complex data structures.

Challenges when working with binary files in Python include data corruption during writes or reads, difficulty in understanding binary data due to lack of visualization, and potential data alignment issues due to platform differences. Mitigating these issues involves implementing thorough error handling, using structured data formats for storage, and exercising caution with file operations to ensure consistent state.

The function countrec() can be used to read contents from 'STUDENT.DAT' and check each student's percentage. It will display details of students whose percentage is above 75% and also count these students, displaying the total count at the end.

To add a new student's data to a binary file, you would use the function insertRec(). This function enables you to input data such as roll number, name, and marks for a student and append it to 'student.dat'.

To search for a student's information by roll number in a binary file, you would use the function searchRollNo(r). This function accepts the roll number as a parameter and searches for the corresponding record in 'student.dat'. If the record is found, it displays the roll number, name, and marks. If it is not found, the function outputs 'No record found'.

To calculate the total salary of employees based on their position from a binary file, you would utilize the function SumSalary(Post). This function reads the contents of 'EMP.dat', filters records by the specified post, and sums the salaries of all employees in that category.

You might also like