Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary File Error
#1
I am trying to create a binary file and save data to it but I get the following error message :

File was loaded in the wrong encoding: 'UTF-8'

I named the file employee.dat and I am using wb and we for reading and writing. Did I create the file wrong maybe? I don't think I have errors in my code. I right-clicked on my source folder and created a new file named employee.dat. Can you look at my code and see if I did  do something wrong? 
import Employee
import pickle

LOOK_UP = 1
ADD = 2
CHANGE = 3
DELETE = 4
QUIT = 5
FILENAME = 'employee.dat'


def load_employees():
file = open(FILENAME, 'rb')
eof_check = False
while not eof_check:
try:
employee_dict = pickle.load(file)
except IOError:
employee_dict = {}
except EOFError:
eof_check = True
file.close()

return employee_dict


def get_user_choice():
print('MENU')
print('1. Look up an employee')
print('2. Add a new employee')
print('3. Change an existing employee')
print('4. Delete an employee')
print('5. Quit the program')
print()

try:
choice = int(input('Enter your choice: '))
while choice < LOOK_UP or choice > QUIT:
choice = int(input('Invalid. Enter your choice: '))
except:
print('An error occurred ')

return choice


def look_up(employee_dict):
id_num = input('Enter an employee ID number: ')
# print(Employee.id_num)
print(employee_dict.get(id_num, 'The specified ID number was not found.'))


def add(employee_dict):
name = input('Enter employee name: ')
id_num = input('Enter employee id number: ')
depart = input('Enter employee department: ')
job = input('Enter employee job title: ')

entry = Employee.Employee(name, id_num, depart, job)

if id_num not in employee_dict:
employee_dict[id_num] = entry
print('The new employee has been added.')
else:
print('An employee with that ID already exists.')


def change(employee_dict):
id_num = input('Enter employee ID number: ')

if id_num in employee_dict:
name = input('Enter employee name: ')
depart = input('Enter employee department: ')
job = input('Enter employee job title: ')

entry = Employee.Employee(name, depart, job)
employee_dict[id_num] = entry
print('Employee info has been changed')
else:
print('The specified ID number was not found')


def delete(employee_dict):
id_num = input('Enter an employee ID number:')

if id_num in employee_dict:
del employee_dict[id_num]
print('Employee deleted')
else:
print('The specified ID number was not found')


def save_employees(employee_dict):
file = open(FILENAME, 'wb')
pickle.dump(employee_dict, file)
file.close()


def main():
emp1 = Employee.Employee('Susan Meyers', 47899, 'Accounting', 'Vice President')
emp2 = Employee.Employee('Mark Jones', 39119, 'IT', 'Programmer')
emp3 = Employee.Employee('Joe Rodgers', 81774, 'Manufacturing', 'Engineer')

my_dict = {47899: emp1,
39119: emp2,
81774: emp3}

save_employees(my_dict)
my_dict = load_employees()

choice = 0
while choice != QUIT:
choice = get_user_choice()

if choice == LOOK_UP:
look_up(my_dict)
elif choice == ADD:
add(my_dict)
elif choice == CHANGE:
change(my_dict)
elif choice == DELETE:
delete(my_dict)

save_employees(my_dict)


if __name__ == '__main__':
main()
Reply
#2
Please use "python" tags for your code. 

Is the error from your python program when it runs, or is it just from your editor/IDE?  Did you create the file from this program or create it some other way?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I read and write a binary file in Python? blackears 6 6,395 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Hashing an address for binary file Python_help 8 2,593 Nov-04-2021, 06:23 AM
Last Post: ndc85430
  Read/Write binary file deanhystad 3 3,163 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  Convert file of hex strings to binary file medatib531 4 13,703 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  Binary File Read Aussie 6 8,335 Sep-03-2020, 03:57 AM
Last Post: deanhystad
  Binary file Aussie 12 4,475 Aug-31-2020, 09:20 PM
Last Post: Aussie
  python read binary file Pyguys 4 3,849 Jul-13-2020, 02:34 AM
Last Post: Pyguys
  Failure in writing binary text to file Gigux 7 3,762 Jul-04-2020, 08:41 AM
Last Post: Gigux
  search binary file and list all founded keyword offset Pyguys 4 2,750 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  hex file to binary or pcap to binary baran01 1 5,676 Dec-11-2019, 10:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020