-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment07.py
More file actions
61 lines (55 loc) · 2.12 KB
/
Assignment07.py
File metadata and controls
61 lines (55 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# ------------------------------------------------- #
# Title: Lab7-1
# Description: A simple example of storing data in a binary file and using Pickling and Structured Error Handling
# ChangeLog: (Who, When, What)
# T.Wang, 17 Nov 2019, Created Script
# <YourName>,<1.1.2030>,Created Script
# ------------------------------------------------- #
import pickle # This imports code from another code file!
# Data--------------------------------------------- #
strFileName = 'HomeInventory.dat'
lstCustomer = []
# Processing--------------------------------------- #
def save_data_to_file(file_name, list_of_data):
while (True):
try:
intID = int(input("Enter an ID: "))
except ValueError as e:
print("Not a valid integer. Please Enter again.")
print("Built-in python error info:")
print(e, e.__doc__, type(e), sep='\n')
try:
strName = str(input("Enter a household item: "))
if strName.isnumeric():
raise Exception('Not a valid entry, need to be an item name')
break
except Exception as e:
print("Built-in python error info:")
print(e, e.__doc__, type(e), sep='\n')
lstCustomer = [intID, strName]
objFile = open(file_name, "wb") # Now we store the data with the pickle.dump method
pickle.dump(lstCustomer, objFile)
objFile.close()
def read_data_from_file(file_name):
objFile = open(file_name, "rb")
list_of_data = pickle.load(objFile)
objFile.close()
return (list_of_data)
# Presentation------------------------------------ #
while (True):
print("""
Menu of Options
1) Add and Save Data to File
2) Show Current Data
3) Exit Program
""")
strChoice = str(input("Which option would you like to perform? [1 to 3] - "))
print() # adding a new line for look
if (strChoice.strip() == '1'):
save_data_to_file(strFileName, lstCustomer)
print("Data Saved!")
elif (strChoice.strip() == '2'):
print(read_data_from_file(strFileName))
elif (strChoice.strip() == '3'):
print('Exit!')
break