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.
- user_input=input('Enter a File name:')
- try:
- fin=open(user_input)
- for line in fin:
- print(str.upper(line),end='')
- fin.close()
- except:
- print('File doesn\'t exist')
- --------------------------------------------------------------------------------------------------------------
- #Exercise 7.2 Write a program to prompt for a file name, and then read through the file and look for lines of the form:
- def spamcount(a,b):
- for line in a:
- l=line.split()
- if l==[]:continue
- if 'X-DSPAM-Confidence:'not in l[0]:continue
- b.append(l[1])
- return b
- ui=input('Enter a File name:')
- lis=[]
- j=0
- try:
- fin=open(ui)
- spamcount(fin,lis)
- for i in lis:
- j=float(i)+j
- print('Average Spam Confidence: ',format(j/len(lis),'.12g'))
- fin.close()
- except:
- print('Oops......file doesn\'t exist')
- --------------------------------------------------------------------------------------------------------------
- #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.
- user=input("Enter a File name:")
- try:
- count=0
- fin=open(user)
- for line in fin:
- if line.startswith('Subject'):
- count+=1
- fin.close()
- print('There were %d subject lines in '% count+'%s' % user)
- except:
- if user=='na na boo boo':
- print("NA NA BOO BOO TO YOU - You have been punk'd!")
- else:
- print("File cannot be opened: ",user)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement