/Demo/scripts/from.py

http://unladen-swallow.googlecode.com/ · Python · 35 lines · 23 code · 6 blank · 6 comment · 11 complexity · 7ac20134b5ce765688685106c4f501ab MD5 · raw file

  1. #! /usr/bin/env python
  2. # Print From and Subject of messages in $MAIL.
  3. # Extension to multiple mailboxes and other bells & whistles are left
  4. # as exercises for the reader.
  5. import sys, os
  6. # Open mailbox file. Exits with exception when this fails.
  7. try:
  8. mailbox = os.environ['MAIL']
  9. except (AttributeError, KeyError):
  10. sys.stderr.write('No environment variable $MAIL\n')
  11. sys.exit(2)
  12. try:
  13. mail = open(mailbox)
  14. except IOError:
  15. sys.exit('Cannot open mailbox file: ' + mailbox)
  16. while 1:
  17. line = mail.readline()
  18. if not line:
  19. break # EOF
  20. if line.startswith('From '):
  21. # Start of message found
  22. print line[:-1],
  23. while 1:
  24. line = mail.readline()
  25. if not line or line == '\n':
  26. break
  27. if line.startswith('Subject: '):
  28. print repr(line[9:-1]),
  29. print