Python Glossary Русский

all Python 3until 3.13Removed from the standard libraryterm

mailcap

removed modules

Read mailcap files: which program opens a given MIME type. Removed in Python 3.13 — use mimetypes for the types.

Syntax

import mailcap  # до 3.13

mailcap.getcaps() collected rules from ~/.mailcap and /etc/mailcap, and mailcap.findmatch(caps, 'image/png', filename=...) returned a command such as 'display photo.png'. PEP 594 dropped it: the format came from 1990s Unix mail clients, and substituting a file name into a command turned into a vulnerability. How to port: mimetypes.guess_type() gives the type from a file name, mimetypes.guess_extension() the extension from a type; to open a file with the default program use os.startfile() on Windows or subprocess.run(['xdg-open', path]) on Linux and ['open', path] on macOS. The standard-mailcap package on PyPI restores the module.

Examples

# Python 3.13+
try:
    import mailcap
except ModuleNotFoundError as e:
    print(type(e).__name__)  # → ModuleNotFoundError
# Python 3.13+
import mimetypes
print(mimetypes.guess_type('photo.png'))  # → ('image/png', None)
print(mimetypes.guess_extension('application/pdf'))  # → .pdf
import mailcap
caps = {'image/png': [{'view': 'display %s'}]}
command, entry = mailcap.findmatch(caps, 'image/png', filename='photo.png')
print(command)  # → display photo.png

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy