Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exercise 7.1
- # Write a program to read through a file
- # and print the contents of the file (line by
- # line) all in upper case. Executing the
- # program will look as follows.
- fname = raw_input('Enter a file name:')
- try:
- fhandle = open(fname)
- except:
- print 'The file name(',fname,') you entered is wrong, try again.'
- exit()
- for line in fhandle:
- print line.rstrip().upper()
- fhandle.close()
- # Exercise 7.2
- # Write a program to prompt for a file name
- # an then read through the file and look
- # for lines of the form: X-DSPAM-Confidence: 0.8475
- # When you encounter a line that starts with
- # "X-DSPAM-Confidence:" pull apart the line to
- # extract the floating point number on the line.
- # Count these lines and the compute the total of
- # the spam confidence values from these lines. When
- # you reach the end of the file, print out the
- # average spam confidence.
- fname = raw_input('Enter the file name:')
- try:
- fhandle = open(fname)
- except:
- print 'The file name(',fname,') you entered is wrong, try again.'
- exit()
- count = 0
- total = 0
- for line in fhandle:
- line = line.rstrip()
- linesinuse = line.startswith('X-DSPAM-Confidence:')
- if linesinuse:
- count += 1
- keep = line[20:]
- total += float(keep)
- div = float(total / count)
- print 'Average spam confidence:',div
- # Exercise 7.3
- # Sometimes when programmers get bored or want to
- # have a bit of fun, they add a harmless Easter Egg to
- # their program (en.wikipedia.org/wiki/Easter_egg_(media)).
- # Modify the program that prompts the user for the file
- # name 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:
- fname = raw_input('Enter the file name:')
- try:
- fhandle = open(fname)
- except:
- if fname == "na na boo boo":
- print 'NA NA BOO BOO TO YOU - You have been punk\'d!'
- else:
- print 'The file name(',fname,') you entered is wrong, try again.'
- exit()
- count = 0
- total = 0
- for line in fhandle:
- line = line.rstrip()
- linesinuse = line.startswith('X-DSPAM-Confidence:')
- if linesinuse:
- count += 1
- keep = line[20:]
- total += float(keep)
- div = float(total / count)
- print 'Average spam confidence:',div
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement