-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment07.py
60 lines (48 loc) · 1.74 KB
/
Assignment07.py
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
# ------------------------------------------------- #
# Title: Assignment07
# Description: Using the pickle module and try/except.
# ChangeLog: (Who, When, What)
# Timothy McDowell, 6.1.2022, Created Script
# ------------------------------------------------- #
# import the new module to use in script
import pickle
message = []
# function for reading the pickle file
def read_pickle(file):
file_obj = open(file, "rb")
data_obj = pickle.load(file_obj)
file_obj.close()
return data_obj
# funtion for writing to the pickle file
def write_pickle(file, data):
file_obj = open(file, "wb")
pickle.dump(data, file_obj)
file_obj.close()
# Option menu with try/except handling
while True:
print(
"\n\nWhat's your choise?\n\n",
"1. Read file\n",
"2. Write to file\n",
"3. Exit program\n"
)
option = int(input("Enter number: "))
if option == 1:
read_file = input("What is the file to read? \n")
try:
print("\n\n///////////////YOUR SECRET MESSAGE IS///////////////////")
print(read_pickle(read_file))
print("////////////////////////////////////////////////////////")
except:
print("That data is not available, pick another file\n\n")
if option == 2:
write_file = input("What's the name of your file? ")
try:
data = input("What's your message? ")
except EOFError:
message.append(data)
write_pickle(write_file, message)
break
if option == 3:
print("Goodbye")
break