-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile slicing
More file actions
38 lines (36 loc) · 1.21 KB
/
File slicing
File metadata and controls
38 lines (36 loc) · 1.21 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
# Sometimes when programmers get bored or want to have a
# bit of fun, they add a harmless Easter Egg to their program.
# Modify Exercise 7.2, so that it prints a
#funny message when the user types in the exact file name “na na boo
#boo”.
# The program should behave normally for all other files which
# exist and don’t exist. Here is a sample execution of the program:
# python egg.py
# Enter the file name: mbox.txt
# There were 1797 subject lines in mbox.txt
# python egg.py
# Enter the file name: missing.tyxt
# File cannot be opened: missing.tyxt
# python egg.py
# Enter the file name: na na boo boo
# NA NA BOO BOO TO YOU - You have been punk'd!
fname = input('Enter file name: ')
count = 0
if fname == 'na na boo boo':
print("NA NA BOO BOO TO YOU - You have been punk'd")
quit()
try:
fhand = open(fname)
except:
print('File cannot be opened: ', fname)
quit()
for line in fhand:
line = line.rstrip()
if line.startswith('X-DSPAM-Confidence:'):
#print(line)
pos = line.find(':')
slice = line[pos+1:]
fslice = float(slice)
#print(fslice)
count = count + 1
print('There were ', count, 'subject lines in', fname)