Course Overview | Previous Session | Next Session
Introduction
In today’s session, we’re going to help the animal shelter manage its employee data using Python. Instead of focusing on coding from scratch, we’ll learn how to use Python to interact with data stored in a
.txt
file. This session is about using existing Python code to work with data and will provide you with tools to look up specific information as needed.
Step 1: Create the Employee Data File
Create a Text File
- Open your text editor.
- Copy and paste the following employee data:
Jack Bossinger;Shelter Manager;555-1234
Susan Boothe;Volunteer Coordinator;555-5678
Bob Johnson;Animal Caretaker;555-8765
Emily Davis;Fundraising Coordinator;555-4321
Michael Lee;Veterinary Technician;555-6789
- Save this file as
employees.txt
in your code directory.
Step 2: Write the Python Script
Create the Python Script
- Open your text editor and create a new file named
fetchEmployee.py
. - Copy and paste the following code into
fetchEmployee.py
:
# fetchEmployee.py
import sys
def fetch_employee(index):
try:
index = int(index)
with open('employees.txt', 'r') as file:
lines = file.readlines()
if index < 1 or index > len(lines):
print(f"Error: Index {index} is out of range. There are only {len(lines)} employees.")
return
# Fetch the line at the specified index (adjusted for 0-based index)
line = lines[index - 1].strip()
name, position, phone = line.split(';')
print(f"Name: {name}")
print(f"Position: {position}")
print(f"Phone: {phone}")
except ValueError:
print("Error: The index must be an integer.")
except FileNotFoundError:
print("Error: The file 'employees.txt' does not exist.")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python fetchEmployee.py <index>")
else:
fetch_employee(sys.argv[1])
- Save the file in the same directory as
employees.txt
.
Step 3: Using the Python Script
Open Your Terminal
- Make sure your terminal (or command prompt) is open.Navigate to the directory where
employees.txt
andfetchEmployee.py
are saved.
Run the Python Script
- To fetch information about a specific employee, type the following command:
python3 fetchEmployee.py <index>
Replace <index>
with the number of the employee you want to look up. For example, to fetch information about the 3rd employee:
python3 fetchEmployee.py 3
Check the Output
- After running the script, you will see details about the specified employee. For the example above, you would see:
Name: Bob Johnson
Position: Animal Caretaker
Phone: 555-8765
Summary
In this session, you’ve learned how to use Python to interact with data from a text file. This approach allows you to query specific information without modifying the script each time. While today’s focus is on using Python to work with data, remember that Python can also be used to create graphical user interfaces, generate charts, and much more, which will be covered in more advanced courses.
Exercise Suggestions
- Experiment with Your Data: Modify the
employees.txt
file to include new data or change existing entries. Use the script to query different employees. - Create Your Own Data Files: Try creating and querying other types of data files, like a book index or song list, using a similar approach.
Keep practicing and exploring different ways to use Python to manage and interact with data!