Консервирование структурированных данных
Почему код возвращает эту ошибку: TypeError: file must have 'read' and 'readline' attributes
function.py
Код:
import pickle
def file_open():
inf = ("pickle.pkl", "rb")
air_list = pickle.load(inf)
inf.close()
return air_list
def file_close(air_list):
fout = len(air_list)
out = open("pickle.pkl", "wb")
pickle.dump(air_list, out, fout)
out.close
def addAirplane(air_list):
list1 = [[],[],[],[],[],[],[]]
list1[0] = input("n Enter ID: ")
list1[1] = input(" Enter destination: ")
list1[2] = input(" Enter flight number: ")
list1[3] = input(" Enter aircraft type: ")
list1[4] = input(" Enter departure time: ")
list1[5] = input(" Enter day of week: ")
air_list.append(list1)
def delete(air_list):
iD = int(input(" ID: "))
del air_list[iD]
def show(air_list):
print("t Information n")
for i in range(len(air_list)):
print("n ID: ", air_list[0])
print(" Destination: ",air_list[1])
print(" Flight number: ",air_list[2])
print(" Aircraft type: ",air_list[3])
print(" Departure time: ",air_list[4])
print(" Day fo week: ",air_list[5])
print("n")
return
def search(list_airplanes):
id = int(input(" Enter ID: "))
print("n")
for i in range(len(air_list)):
if(id == int(air_list[i][0])):
print("n ID: ", air_list[i][0])
print(" Destination: ",air_list[i][1])
print(" Flight number: ",air_list[i][2])
print(" Aircraft type: ",air_list[i][3])
print(" Departure time: ",air_list[i][4])
print(" Day fo week: ",air_list[i][5])
print("n")
def sorting(air_list, x):
air_list1 = []
m = 0
for i in air_list:
sotr_el = []
sort_el.append(m)
sort_el.append(int(i[x]))
list_air.append(sort)
m += 1
air_list1.sort(key=lambda air_list1:air_list1[1])
for i in range(len(air_list1)):
print(" ID: ",air_list1[i][0])
print(" Destination: ",air_list1[i][0][0])
print(" Flight number: ",air_list1[i][0][1])
print(" Aircraft type: ",air_list[i][0][2])
print(" Departure time: ",air_list1[i][0][3])
print(" Day fo week: ",air_list1[i][0][4])
print("n")
return
def menu():
n = int(input("""ntMenu: n
1. Show all the record
2. Add record
3. Delete record
4. Search records
5. Sort of records
6. Exit
Enter your choice: """))
return n
def file_open():
inf = ("pickle.pkl", "rb")
air_list = pickle.load(inf)
inf.close()
return air_list
def file_close(air_list):
fout = len(air_list)
out = open("pickle.pkl", "wb")
pickle.dump(air_list, out, fout)
out.close
def addAirplane(air_list):
list1 = [[],[],[],[],[],[],[]]
list1[0] = input("n Enter ID: ")
list1[1] = input(" Enter destination: ")
list1[2] = input(" Enter flight number: ")
list1[3] = input(" Enter aircraft type: ")
list1[4] = input(" Enter departure time: ")
list1[5] = input(" Enter day of week: ")
air_list.append(list1)
def delete(air_list):
iD = int(input(" ID: "))
del air_list[iD]
def show(air_list):
print("t Information n")
for i in range(len(air_list)):
print("n ID: ", air_list[0])
print(" Destination: ",air_list[1])
print(" Flight number: ",air_list[2])
print(" Aircraft type: ",air_list[3])
print(" Departure time: ",air_list[4])
print(" Day fo week: ",air_list[5])
print("n")
return
def search(list_airplanes):
id = int(input(" Enter ID: "))
print("n")
for i in range(len(air_list)):
if(id == int(air_list[i][0])):
print("n ID: ", air_list[i][0])
print(" Destination: ",air_list[i][1])
print(" Flight number: ",air_list[i][2])
print(" Aircraft type: ",air_list[i][3])
print(" Departure time: ",air_list[i][4])
print(" Day fo week: ",air_list[i][5])
print("n")
def sorting(air_list, x):
air_list1 = []
m = 0
for i in air_list:
sotr_el = []
sort_el.append(m)
sort_el.append(int(i[x]))
list_air.append(sort)
m += 1
air_list1.sort(key=lambda air_list1:air_list1[1])
for i in range(len(air_list1)):
print(" ID: ",air_list1[i][0])
print(" Destination: ",air_list1[i][0][0])
print(" Flight number: ",air_list1[i][0][1])
print(" Aircraft type: ",air_list[i][0][2])
print(" Departure time: ",air_list1[i][0][3])
print(" Day fo week: ",air_list1[i][0][4])
print("n")
return
def menu():
n = int(input("""ntMenu: n
1. Show all the record
2. Add record
3. Delete record
4. Search records
5. Sort of records
6. Exit
Enter your choice: """))
return n
Код:
import pickle
from function import *
air_list = []
while True:
air_list = file_open()
n = menu()
if (n == 1):
show(air_list)
if (n == 2):
addAirplane(air_list)
if (n == 3):
delete(air_list)
if (n == 4):
search(air_list)
if (n == 5):
x = int(input(("n 0 - Sorting by ID,n 1 - Sorting by fligh number)n")))
sorting(air_list, x)
if (n == 6):
break
file_close(air_list)
input(" Press enter for exit...")
from function import *
air_list = []
while True:
air_list = file_open()
n = menu()
if (n == 1):
show(air_list)
if (n == 2):
addAirplane(air_list)
if (n == 3):
delete(air_list)
if (n == 4):
search(air_list)
if (n == 5):
x = int(input(("n 0 - Sorting by ID,n 1 - Sorting by fligh number)n")))
sorting(air_list, x)
if (n == 6):
break
file_close(air_list)
input(" Press enter for exit...")