-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTry and except 2
More file actions
38 lines (36 loc) · 968 Bytes
/
Try and except 2
File metadata and controls
38 lines (36 loc) · 968 Bytes
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
# Rewrite the guardian code in the code below
#
# file = input('Enter file name: ')
# try :
# fhand = open(file)
# except:
# print('File name not found. Please enter a new file name')
# quit()
# count = 0
# for line in fhand:
# words = line.split()
# # print('Debug:', words)
# if len(words) == 0:
# continue
# if words[0] != 'From':
# continue
# # print(words)
# print(words[2])
#
# without the two if statements. Instead, use a compound logical expression using
# the or logical operator with a single if statement.
file = input('Enter file name: ')
try:
fhand = open(file)
except:
print('File name not found. Please enter a new file name')
quit()
count = 0
for line in fhand:
words = line.split()
# print('Debug:', words)
if len(words) == 0 or words[0] != 'From':
continue
# print(words)
count = count + 1
print(count, words[2])