-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch6_4_exceptions_reading_file.py
58 lines (41 loc) · 1.5 KB
/
ch6_4_exceptions_reading_file.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
# chapter 6 reboot 5/2/21 Saturday to beat Lab 4 to death
# replay lectures from 3/30 Tuesday
def main:
################ 6.4 3/30/21 lecture Exceptions ##########
# this program will read the records out of the file then display them
try:
#open the file
friends = open('friends2.txt', 'r')
# read first line name field
name = friends.readline()
#if a field is read establish records
while name != "":
#strip \n from name field
name = name.rstrip('\n')
#convert strin age to interger
age = int(friends.readline())
#strip \n from food field
food = friends.readline().rstrip('\n')
print(name, age, food)
name = friends.readline()
friends.close()
#file not found exception
except FileNotFoundError:
print("File was not found")
except Exception as err:
print("Error:", err)
main()
################ 6.4 3/30/21 lecture Exceptions ##########
## try: #signals start of exception
## num = int(input("Enter an integer: "))
##
## print("You entered", num)
##
## result = num / 0
##
## except ValueError: #exception at input
## print("Invalid value - must be number")
## except TypeError: #exception with int function
## print("Error calling the input frunction")
## except as error: #name the error
## print("An unknown error has occurred")