Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3.2
- # -*- coding: utf-8 -*-
- # main.py
- """ Task: Exercise 8.2
- Figure out which line of the above program is still not properly guarded.
- See if you can construct a text file which causes the program to fail and
- then modify the program so that the line is properly guarded and test it
- to make sure it handles your new text file.
- """
- # test.txt is equal to mbox.txt, but added following 3 lines somewhere:
- """
- >>> The next line fails with the original code:
- From xyz
- >>> because of there are more than one, but less than two words...
- """
- ''' Main '''
- try:
- fhand = open('mbox.txt')
- except:
- print("File not found!")
- exit()
- week = ["Sat", "Sun", "Mon", "Tue", "Wed", "Fri"]
- count = 0
- for line in fhand:
- words = line.split()
- # print('Debug:', words)
- if len(words) <= 2: continue # when there are less than 3 words
- if words[0] != 'From': continue
- if words[2] not in week[:]: continue
- try: # Additional guard
- print(words[2])
- count += 1 # counts the interesting lines
- except IndexError:
- continue
- print("There are " + str(count) + " interesting lines.")
- try:
- fhand.close()
- except:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement