Lab07 (33821996)
Lab07 (33821996)
Exercise 1:
Write a program to read through a file and print the contents of the file (line by line), all in upper
case.
Sample Output:
Enter the file name: mbox-short.txt
FROM [email protected] SAT JAN 5 09:14:16 2008
RETURN-PATH: <[email protected]>
RECEIVED: FROM MURDER (MAIL.UMICH.EDU [141.211.14.90])
BY FRANKENSTEIN.MAIL.UMICH.EDU (CYRUS V2.3.8) WITH LMTPA;
SAT, 05 JAN 2008 09:14:16 -0500
X-SIEVE: CMU SIEVE 2.3
RECEIVED: FROM MURDER ([UNIX SOCKET])
. . .
Exercise 2:
Develop a program that opens the file “mbox-short.txt”. After opening the file, the program should
count and show email addresses that come after the word “From” in the file.
Sample Output:
Enter the file name: mbox-short.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
. . .
[email protected]
The number of contacted email addresses: 27
Exercise 3:
Develop a program that opens the file “mbox-short.txt” by asking a user to enter the file name.
• If the file name is incorrectly entered, the program should inform the user about it and ask to
enter the file name again.
• When the file name is entered correctly, the program should print out the lines that show
“New Revision:” numbers and the lines that come right before “New Revision:” lines
(starting with “Date:”).
Sample Output:
Enter the file name: mbox-short.txt
Date: 2008-01-05 09:12:07 -0500 (Sat, 05 Jan 2008)
New Revision: 39772
Date: 2008-01-04 18:08:50 -0500 (Fri, 04 Jan 2008)
New Revision: 39771
Date: 2008-01-04 16:09:01 -0500 (Fri, 04 Jan 2008)
New Revision: 39770
Date: 2008-01-04 15:44:39 -0500 (Fri, 04 Jan 2008)
New Revision: 39769
Date: 2008-01-04 15:01:37 -0500 (Fri, 04 Jan 2008)
. . .
Exercise 4:
Write a program to prompt for a file name, and then read through the file and look for lines of the
form like:
X-DSPAM-Confidence: 0.8475
In this program, you have to take all ‘X-DSPAM-Confidence’ values (ex.: 0.8475, 0.6178, …) and
print out the average spam confidence value as an output.
Sample Output:
Enter the file name: mbox-short.txt
Average spam confidence: 0.750718518519
Exercise 5:
Write a program that opens the file “mbox-short.txt” by asking a user to enter the file name, write
all email addresses of the line starting with “Author:” to a newly created file with the name
“authors.txt”, and close the files.
Then, open the file “authors.txt” and append the total number of authors from Michigan University
(umich.edu) in this file.
Sample Output:
Exercise 6:
A university secretary makes available to a professor a text file (named “students.txt”) where, for
each row, the name and the student ID number are reported. At the end of the course, the professor
provides another file (“grades.txt”) in which he writes, for each line, the student ID number and the
two marks the student obtained. No correspondence between the rows of the two files can be
assumed.
Develop a program that, by reading those files, prints out each student’s name and his/her average
GPA score.