We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

The mailcap module

This module contains code to deal with “mailcap” files, which contain information on how to deal with different document formats (on Unix platforms).

Example: Using the mailcap module to get a capability dictionary
# File: mailcap-example-1.py

import mailcap

caps = mailcap.getcaps()

for k, v in caps.items():
    print k, "=", v

image/* = [{'view': 'pilview'}]
application/postscript = [{'view': 'ghostview'}]

In the above example, the system uses pilview for all kinds of images, and ghostscript viewer for PostScript documents.

Example: Using the mailcap module to find a viewer
# File: mailcap-example-2.py

import mailcap

caps = mailcap.getcaps()

command, info = mailcap.findmatch(
    caps, "image/jpeg", "view", "samples/sample.jpg"
    )

print command

pilview samples/sample.jpg