Untitled
unknown
plain_text
25 days ago
6.0 kB
3
Indexable
import os def calculate_average_scores(dept_file): """ Calculates the average scores for Task 1, Task 2, Mid-Year Review, and Final Evaluation for a given department file. Args: dept_file (str): The name of the department file. Returns: tuple: A tuple containing the averages for Task 1, Task 2, Mid-Year Review, and Final Evaluation, or None if the file is empty or an error occurs. """ task1_scores = [] task2_scores = [] mid_year_scores = [] final_eval_scores = [] try: with open(dept_file, 'r') as file: for line in file: scores = line.strip().split() if len(scores) == 5: try: task1_scores.append(float(scores[0])) task2_scores.append(float(scores[1])) mid_year_scores.append(float(scores[2])) final_eval_scores.append(float(scores[3])) except ValueError: print(f"Warning: Invalid data format in line: {line.strip()} in file {dept_file}") else: print(f"Warning: Incomplete data in line: {line.strip()} in file {dept_file}") if task1_scores: avg_task1 = sum(task1_scores) / len(task1_scores) avg_task2 = sum(task2_scores) / len(task2_scores) avg_mid_year = sum(mid_year_scores) / len(mid_year_scores) avg_final_eval = sum(final_eval_scores) / len(final_eval_scores) return avg_task1, avg_task2, avg_mid_year, avg_final_eval else: print(f"Warning: No data found in {dept_file}") return None except FileNotFoundError: print(f"Error: File not found: {dept_file}") return None except Exception as e: print(f"An error occurred processing {dept_file}: {e}") return None def find_low_performers(dept_file): """ Determines the employee IDs who did not meet the performance threshold (60 out of 100) for a given department file. Args: dept_file (str): The name of the department file. Returns: list: A list of employee IDs who did not meet the threshold, or an empty list if the file is empty or an error occurs. """ low_performers = [] try: with open(dept_file, 'r') as file: for line in file: scores = line.strip().split() if len(scores) == 5: try: task1 = float(scores[0]) * 0.20 task2 = float(scores[1]) * 0.30 mid_year = float(scores[2]) * 0.25 final_eval = float(scores[3]) * 0.25 total_score = task1 + task2 + mid_year + final_eval employee_id = scores[4] if total_score < 60: low_performers.append(employee_id) except ValueError: print(f"Warning: Invalid data format in line: {line.strip()} in file {dept_file}") else: print(f"Warning: Incomplete data in line: {line.strip()} in file {dept_file}") except FileNotFoundError: print(f"Error: File not found: {dept_file}") except Exception as e: print(f"An error occurred processing {dept_file}: {e}") return low_performers def search_keyword_in_report(report_file): """ Allows the user to enter a keyword and searches for it in a case-insensitive manner in the company performance report, counting its occurrences. Args: report_file (str): The name of the company performance report file. """ try: with open(report_file, 'r') as file: lines = file.readlines() while True: keyword = input("Enter a keyword to search for (or 'exit' to quit): ").strip() if keyword.lower() == 'exit': break count = 0 for line in lines: count += line.lower().count(keyword.lower()) print(f"The keyword '{keyword}' appears {count} times in the report.") except FileNotFoundError: print(f"Error: File not found: {report_file}") except Exception as e: print(f"An error occurred processing {report_file}: {e}") def process_department_files(): """ Processes all department files, calculates average scores, and identifies low performers. """ for filename in os.listdir('.'): # Assuming files are in the same directory if filename.startswith("DEPT_") and filename.endswith(".txt"): dept_code = filename.split('.')[0] # Calculate and save average scores averages = calculate_average_scores(filename) if averages: avg_filename = f"{dept_code}_avg.txt" with open(avg_filename, 'w') as outfile: outfile.write(f"Task 1 Average: {averages[0]:.2f}\n") outfile.write(f"Task 2 Average: {averages[1]:.2f}\n") outfile.write(f"Mid-Year Review Average: {averages[2]:.2f}\n") outfile.write(f"Final Evaluation Average: {averages[3]:.2f}\n") print(f"Average scores saved to {avg_filename}") # Find and save low performers low_performers = find_low_performers(filename) if low_performers: low_perf_filename = f"{dept_code}_low_perf.txt" with open(low_perf_filename, 'w') as outfile: outfile.write("Employee IDs of low performers:\n") for emp_id in low_performers: outfile.write(f"{emp_id}\n") print(f"Low performers list saved to {low_perf_filename}") if __name__ == "__main__": process_department_files() search_keyword_in_report("company_performance_report.txt")
Editor is loading...
Leave a Comment