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 9.5
- This programrecords the domain name (instead of the address) where the
- message was sent from instead of who the mail came from (i.e. the whole
- e-mail address). At the end of the program print out the contents of your
- dictionary.
- python schoolcount.py
- Enter a file name: mbox-short.txt
- {'media.berkeley.edu': 4, 'uct.ac.za': 6, 'umich.edu': 7,
- 'gmail.com': 1, 'caret.cam.ac.uk': 1, 'iupui.edu': 8}
- """
- ''' Functions '''
- def enterFileName():
- """
- The user has to enter a filename.
- Returns fileName
- """
- fileName = None
- while fileName == None:
- # Enter filename
- try:
- fileName = input("Enter the filename: ")
- except:
- print("Invalid input!")
- continue
- return fileName
- ''' Main '''
- # Open file
- fileName = enterFileName()
- try:
- fhand = open(fileName)
- except:
- print("File not found!")
- exit()
- domains = dict()
- for line in fhand:
- words = line.split()
- # print('Debug:', words)
- if len(words) >= 2 and words[0] == 'From':
- # Cut the word till '@' (inclusive)
- words[1] = words[1][words[1].find('@') + 1:]
- # Update dictionary
- domains[words[1]] = domains.get(words[1], 0) + 1
- # Print-out
- for i in domains:
- #if domains[i] == max(domains.values()):
- print(str(i) + " " + str(domains[i]))
- # Close file
- try:
- fhand.close()
- except:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement