The mailcap Module

The mailcap module in Example 6-7 contains code that deals with mailcap files, which contain information on how to handle different document formats (on Unix platforms).

Example 6-7. 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 Example 6-7, the system uses pilview for all kinds of images, and ghostscript viewer for PostScript documents. Example 6-8 shows how to find a viewer using mailcap.

Example 6-8. 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

Get Python Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.