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.4
- Write a program to read through a mail log, and figure out who had the most
- messages in the file. The program looks for “From” lines and takes the
- second parameter on those lines as the person who sent the mail.
- The program creates a Python dictionary that maps the sender’s address to
- the total number of messages for that person.
- After all the data has been read the program looks through the dictionary
- using a maximum loop (see Section 5.7.2) to find who has the most messages
- and how many messages the person has.
- Enter a file name: mbox-short.txt
- cwen@iupui.edu 5
- Enter a file name: mbox.txt
- zqian@umich.edu 195
- """
- ''' 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()
- sender = dict()
- for line in fhand:
- words = line.split()
- # print('Debug:', words)
- if len(words) >= 2 and words[0] == 'From':
- sender[words[1]] = sender.get(words[1], 0) + 1
- # Print-out
- for i in sender:
- if sender[i] == max(sender.values()):
- print(str(i) + " " + str(sender[i]))
- # Close file
- try:
- fhand.close()
- except:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement