Shared MIME-info Database

XDG Shared MIME-info Database specification

Finding a file’s Mime type


>>> Mime.get_type2('/path/to/')
MIMEtype('application', 'zip')
xdg.Mime.get_type2(path, follow=True)[source]

Find the MIMEtype of a file using the XDG recommended checking order.

This first checks the filename, then uses file contents if the name doesn’t give an unambiguous MIMEtype. It can also handle special filesystem objects like directories and sockets.

  • path – file path to examine (need not exist)
  • follow – whether to follow symlinks
Return type:


New in version 1.0.


Returns type of file by its name, or None if not known

xdg.Mime.get_type_by_contents(path, max_pri=100, min_pri=0)[source]

Returns type of file by its contents, or None if not known

xdg.Mime.get_type_by_data(data, max_pri=100, min_pri=0)[source]

Returns type of the data, which should be bytes.

xdg.Mime.get_type(path, follow=True, name_pri=100)[source]

Returns type of file indicated by path.

This function is deprecated - get_type2() is more accurate.

  • path – pathname to check (need not exist)
  • follow – when reading file, follow symbolic links
  • name_pri – Priority to do name matches. 100=override magic

This tries to use the contents of the file, and falls back to the name. It can also handle special filesystem objects like directories and sockets.

Installing Mime data

xdg.Mime.install_mime_info(application, package_file)[source]

Copy ‘package_file’ as ~/.local/share/mime/packages/<application>.xml. If package_file is None, install <app_dir>/<application>.xml. If already installed, does nothing. May overwrite an existing file with the same name (if the contents are different)

MIMEtype objects

class xdg.Mime.MIMEtype[source]

Class holding data about a MIME type.

Calling the class will return a cached instance, so there is only one instance for each MIME type. The name can either be passed as one part (‘text/plain’), or as two (‘text’, ‘plain’).

Changed in version 1.0: The class now takes care of caching; call lookup() in earlier versions.


e.g. ‘text’


e.g. ‘plain’


Returns comment for current language, loading it if needed.


Returns the canonical MimeType object if this is an alias.


Returns a set of Mime types which this inherits from.

xdg.Mime.lookup(media, subtype=None)[source]

Get the MIMEtype object for the given type.

This remains for backwards compatibility; calling MIMEtype now does the same thing.

The name can either be passed as one part (‘text/plain’), or as two (‘text’, ‘plain’).



Retrieve the set of filename extensions matching a given MIMEtype.

Extensions are returned without a leading dot, e.g. ‘py’. If no extensions are registered for the MIMEtype, returns an empty set.

The extensions are stored in a cache the first time this is called.

New in version 1.0.


Guess whether a file contains text or binary data.

Heuristic: binary if the first 32 bytes include ASCII control characters. This rule may change in future versions.

New in version 1.0.