I wrote this python-script and everytime I run it, it is supposed to input some details that it has to take in addEmployee() method. When I press Enter to submit all the information it throws the following error:
Traceback (most recent call last):
File "C:\Users\ammar\PycharmProjects\pythonProject\main.py", line 122, in <module>
menu()
File "C:\Users\ammar\PycharmProjects\pythonProject\main.py", line 108, in menu
addEmployee()
File "C:\Users\ammar\PycharmProjects\pythonProject\main.py", line 35, in addEmployee
employees = pickle.load(f)
^^^^^^^^^^^^^^
EOFError: Ran out of input
Process finished with exit code 1
Additional information: I'm using Pycharm to do this and I have made a text file called employees.txt in the same folder where this program is stored
I don't know how to fix this any help would be appreciated.
This is my script:
import datetime
import pickle
def addEmployee():
fullName = input("Enter full name: ")
employeeID = int(input("Enter employee ID: "))
department = input("Enter department: ")
doj = input("Enter date of joining (MM/DD/YYYY): ")
salary = int(input("Enter annual salary: "))
if len(fullName) == 0:
print("Please enter full name.")
return
if not 10000 <= employeeID <= 99999:
print("Employee ID should be in the range of 10000 and 99999.")
return
if department not in ["Marketing", "Finance", "Human Resource", "Technical"]:
print("Department should be Marketing, Finance, Human Resource, or Technical.")
return
try:
datetime.datetime.strptime(doj, "%m/%d/%Y")
except ValueError:
print("Invalid date of joining format.")
return
if not 30000 <= salary <= 200000:
print("Salary should be in the range of 30000 and 200000.")
return
with open("employees.txt", "rb") as f:
employees = pickle.load(f)
if employeeID in employees:
print("Employee with ID {} already exists.".format(employeeID))
return
employees.append({
"fullName": fullName,
"employeeID": employeeID,
"department": department,
"doj": doj,
"salary": salary
})
with open("employees.txt", "wb") as f:
pickle.dump(employees, f)
print("Employee added successfully.")
def displayEmployees():
with open("employees.txt", "rb") as f:
employees = pickle.load(f)
if len(employees) == 0:
print("No employees found.")
return
print("Employees:")
for employee in employees:
print(employee)
def deleteEmployee():
employeeID = int(input("Enter employee ID to delete: "))
with open("employees.txt", "rb") as f:
employees = pickle.load(f)
if employeeID not in employees:
print("Employee with ID {} does not exist.".format(employeeID))
return
employees.remove({"employeeID": employeeID})
with open("employees.txt", "wb") as f:
pickle.dump(employees, f)
print("Employee deleted successfully.")
def updateEmployee():
employeeID = int(input("Enter employee ID to update: "))
with open("employees.txt", "rb") as f:
employees = pickle.load(f)
if employeeID not in employees:
print("Employee with ID {} does not exist.".format(employeeID))
return
updatedSalary = None
updatedDepartment = None
print("What do you want to update?")
def menu():
print("1 to Add Employee")
print("2 to Delete Employee")
print("3 to Update Employee")
print("4 to Display Employees")
print("5 to Exit")
ch = int(input("Enter your Choice:"))
if ch == 1:
addEmployee()
elif ch == 2:
deleteEmployee()
elif ch == 3:
updateEmployee()
elif ch == 4:
displayEmployees()
elif ch == 5:
exit(0)
else:
print("Invalid Input")
menu()
menu()
Thanks in advance!
You are facing
EOFError: Ran out of inputerror because you're trying to load a generic.txtfile as a.pklobject. The file name should be asemployees.pkl. Here's your corrected code:Output:
Also, I've made a few modifications to the code. You don't have to manually create a
.pklfile, the first time you are running the code. This block of code will create an empty pickle file when you're running the script for the first time. From then, new entries will be saved to this 'employees.pkl` file.