Improved some error messages for command line processing.
[python/dscho.git] / Demo / scripts / from.py
blob9f749aea60ec855c2e817213bec384bf8b64cc2e
1 #! /usr/bin/env python
3 # Print From and Subject of messages in $MAIL.
4 # Extension to multiple mailboxes and other bells & whistles are left
5 # as exercises for the reader.
7 import sys, os
9 # Open mailbox file. Exits with exception when this fails.
11 try:
12 mailbox = os.environ['MAIL']
13 except (AttributeError, KeyError):
14 sys.stderr.write('No environment variable $MAIL\n')
15 sys.exit(2)
17 try:
18 mail = open(mailbox, 'r')
19 except IOError:
20 sys.stderr.write('Cannot open mailbox file: ' + mailbox + '\n')
21 sys.exit(2)
23 while 1:
24 line = mail.readline()
25 if not line: break # EOF
26 if line[:5] == 'From ':
27 # Start of message found
28 print line[:-1],
29 while 1:
30 line = mail.readline()
31 if not line: break # EOF
32 if line == '\n': break # Blank line ends headers
33 if line[:8] == 'Subject:':
34 print `line[9:-1]`,
35 print