This content originally appeared on DEV Community and was authored by Daniel Azevedo
Welcome to Lesson 4 of Python from 0 to Hero! In the previous lesson, we learned how to manage employee data using lists and dictionaries. Now, it's time to make our scripts even more practical by saving and loading data to and from files. This is essential for automating HR and payroll processes, where employee information needs to be stored securely and accessed efficiently.
By the end of this lesson, you will know how to:
- Read data from files.
- Write data to files.
- Use files to store employee records.
Let’s dive in!
1. Why File Handling is Important for HR Systems
In a real-world HR department, employee data is often stored in files, whether it's a text file, CSV file, or even a database. Automating these tasks with Python can save hours of manual data entry. Here are a few examples of when file handling comes in handy:
- Storing employee details like names, IDs, salaries, and hours worked.
- Loading payroll data to calculate wages and taxes.
- Saving monthly payroll reports for auditing.
Let’s start by learning how to read and write files using Python.
2. Writing Employee Data to a File
Basic File Writing
To save employee data, we can use Python’s open()
function with the "w"
(write) mode. This creates a new file or overwrites an existing one.
Here’s an example where we write the employee names and salaries to a text file:
# Employee dictionary with names and salaries
employees = {
101: {"name": "Alice", "salary": 5000},
102: {"name": "Bob", "salary": 4000},
103: {"name": "Diana", "salary": 4500}
}
# Open a file for writing
with open("employee_data.txt", "w") as file:
# Write header
file.write("Employee ID, Name, Salary\n")
# Write each employee's data to the file
for emp_id, details in employees.items():
file.write(f"{emp_id}, {details['name']}, {details['salary']}\n")
print("Employee data written to file.")
How It Works:
- We create a file called
employee_data.txt
using theopen()
function with the mode"w"
(write). - Inside a
with
block (which automatically closes the file after writing), we first write a header. - Then we loop through the
employees
dictionary, writing each employee’s ID, name, and salary to the file.
Output in the File:
Employee ID, Name, Salary
101, Alice, 5000
102, Bob, 4000
103, Diana, 4500
This is a great way to store employee information for later use.
3. Reading Employee Data from a File
Now, let’s learn how to read the employee data from the file we just created. We’ll use the "r"
(read) mode with the open()
function to do this.
Basic File Reading
Here’s how we can read and display the employee data:
# Open the file for reading
with open("employee_data.txt", "r") as file:
# Read the header
header = file.readline()
print(header.strip())
# Read each line in the file
for line in file:
print(line.strip())
How It Works:
- We open the file in
"r"
(read) mode. - The
readline()
method reads the first line (header) of the file. - We then loop through the remaining lines and display each one.
Output:
Employee ID, Name, Salary
101, Alice, 5000
102, Bob, 4000
103, Diana, 4500
This is useful for loading employee data into your Python program for further processing.
4. Practical Example: Updating Employee Data
Let’s create a practical scenario where we:
- Read employee data from a file.
- Update an employee’s salary based on their ID.
- Write the updated data back to the file.
Full Script: Updating Employee Salaries
# Function to read employee data from a file and store it in a dictionary
def read_employee_data(filename):
employees = {}
with open(filename, "r") as file:
# Skip the header
file.readline()
# Read each employee's data
for line in file:
emp_id, name, salary = line.strip().split(", ")
employees[int(emp_id)] = {"name": name, "salary": float(salary)}
return employees
# Function to write updated employee data back to the file
def write_employee_data(filename, employees):
with open(filename, "w") as file:
file.write("Employee ID, Name, Salary\n")
for emp_id, details in employees.items():
file.write(f"{emp_id}, {details['name']}, {details['salary']:.2f}\n")
# Read employee data from the file
employees = read_employee_data("employee_data.txt")
# Update salary for employee with ID 102 (Bob)
employees[102]["salary"] = 4200
print(f"Updated Bob's salary to ${employees[102]['salary']:.2f}")
# Write the updated data back to the file
write_employee_data("employee_data.txt")
print("Employee data updated.")
Script Breakdown:
- We define a
read_employee_data()
function that reads employee data from a file and stores it in a dictionary. - The
write_employee_data()
function writes the updated dictionary back to the file. - We update Bob’s salary by accessing his entry in the dictionary and modifying the value.
- Finally, we write the updated data back to the file.
Output in the File After Update:
Employee ID, Name, Salary
101, Alice, 5000.00
102, Bob, 4200.00
103, Diana, 4500.00
This demonstrates how you can manage employee records dynamically and store the results persistently.
5. Handling Large Data: CSV Files
For larger datasets, such as hundreds or thousands of employee records, it’s common to use CSV (Comma-Separated Values) files. Python provides a csv
module that makes reading and writing these files much easier and faster.
Example: Writing and Reading CSV Files
Let’s extend our previous example to work with CSV files.
Writing Employee Data to a CSV File:
import csv
# Write employee data to a CSV file
with open("employee_data.csv", mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Employee ID", "Name", "Salary"])
for emp_id, details in employees.items():
writer.writerow([emp_id, details["name"], details["salary"]])
print("Employee data written to CSV file.")
Reading Employee Data from a CSV File:
# Read employee data from a CSV file
with open("employee_data.csv", mode="r") as file:
reader = csv.reader(file)
# Skip the header
next(reader)
# Read each row
for row in reader:
print(row)
CSV File Output:
Employee ID, Name, Salary
101, Alice, 5000.0
102, Bob, 4200.0
103, Diana, 4500.0
Using the csv
module allows you to handle large datasets more efficiently, especially when you need to share or export data to other systems.
Conclusion
In this lesson, we learned how to:
- Write employee data to text files and CSV files for persistent storage.
- Read employee data from files and use it in Python scripts.
- Create a simple employee data update system by reading from and writing to files.
These file-handling skills are crucial for automating data management tasks in HR and payroll systems. In the next lesson, we’ll explore error handling to make your programs more robust when dealing with unexpected issues like missing files or invalid data.
Feel free to share your thoughts and questions in the comments! Keep practicing, and see you in the next lesson!
This content originally appeared on DEV Community and was authored by Daniel Azevedo
Daniel Azevedo | Sciencx (2024-09-11T20:04:03+00:00) Lesson 4 – File Handling for Storing Employee Data. Retrieved from https://www.scien.cx/2024/09/11/lesson-4-file-handling-for-storing-employee-data/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.