KnowledgeBoat Logo
|

Computer Science

Surya is a manager working in a recruitment agency. He needs to manage the records of various candidates. For this, he wants the following information of each candidate to be stored:

  • Candidate_ID – integer
  • Candidate_Name – string
  • Designation – string
  • Experience – float

You, as a programmer of the company, have been assigned to do this job for Surya.

(I) Write a function to input the data of a candidate and append it in a binary file.

(II) Write a function to update the data of candidates whose experience is more than 10 years and change their designation to "Senior Manager".

(III) Write a function to read the data from the binary file and display the data of all those candidates who are not "Senior Manager".

Python File Handling

11 Likes

Answer

(I)

import pickle 
 
def input_candidates(): 
    candidates = [] 
    n = int(input("Enter the number of candidates you want to add: ")) 
    for i in range(n): 
        candidate_id = int(input("Enter Candidate ID: ")) 
        candidate_name = input("Enter Candidate Name: ") 
        designation = input("Enter Designation: ") 
        experience = float(input("Enter Experience (in years): ")) 
        candidates.append([candidate_id, candidate_name, designation, 
experience]) 
    return candidates

candidates_list = input_candidates() 
 
def append_candidate_data(candidates): 
    with open('candidates.bin', 'ab') as file: 
        for candidate in candidates: 
            pickle.dump(candidate, file) 
    print("Candidate data appended successfully.") 
 
append_candidate_data(candidates_list)

(II)

import pickle 
 
def update_senior_manager(): 
    updated_candidates = [] 
    try: 
        with open('candidates.bin', 'rb') as file: 
            while True: 
                try: 
                    candidate = pickle.load(file) 
                    if candidate[3] > 10:  # If experience > 10 years 
                        candidate[2] = 'Senior Manager' 
                    updated_candidates.append(candidate) 
                except EOFError:
                    break  # End of file reached 
    except FileNotFoundError: 
        print("No candidate data found. Please add candidates first.") 
        return 
 
    with open('candidates.bin', 'wb') as file: 
        for candidate in updated_candidates: 
            pickle.dump(candidate, file) 
 
    print("Candidates updated to Senior Manager where applicable.")

update_senior_manager() 

(III)

import pickle 
 
def display_non_senior_managers(): 
    try: 
        with open('candidates.bin', 'rb') as file: 
            while True: 
                try: 
                    candidate = pickle.load(file) 
                    if candidate[2] != 'Senior Manager':  # Check if not Senior Manager 
                        print(f"Candidate ID: {candidate[0]}") 
                        print(f"Candidate Name: {candidate[1]}") 
                        print(f"Designation: {candidate[2]}") 
                        print(f"Experience: {candidate[3]}") 
                        print("--------------------") 
                except EOFError: 
                    break  # End of file reached 
    except FileNotFoundError: 
        print("No candidate data found. Please add candidates first.") 
 
display_non_senior_managers()

Answered By

2 Likes


Related Questions